Skip to content
Permalink
2f63e73366
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
46 lines (37 sloc) 1.12 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[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;
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)
);
}
}