Skip to content
Permalink
0741e87556
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
50 lines (39 sloc) 1.16 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public Boundary boundary;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
public Text score;
void Update()
{
if (Input.GetKeyDown("space") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
rigidbody.velocity = new Vector2(moveHorizontal, moveVertical) * speed;
rigidbody.position = new Vector2
(
Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp(rigidbody.position.y, boundary.yMin, boundary.yMax)
);
}
}