I’m having a weird issue where my laser only Instantiates some of the time I press the fire button. I’m pretty sure I got the code right, but the button press is very unresponsive. Does anyone have an idea what I’m missing?
//configuration parameters
[SerializeField] float speed = 5f;
[SerializeField] float clampPadding = 0.4f;
[SerializeField] float laserSpeed = 5f;
//references
private Rigidbody2D myRigidbody;
[SerializeField] private GameObject myLaserPrefab1;
float xMin;
float xMax;
float yMin;
float yMax;
// Start is called before the first frame update
void Start()
{
//myRigidbody = GetComponent<Rigidbody2D>();
SetUpMoveBoundaries();
}
private void SetUpMoveBoundaries()
{
Camera gameCamera = Camera.main;
xMin = gameCamera.ViewportToWorldPoint(new Vector3(0,0,0)).x;
xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y;
yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y;
}
// Update is called once per frame
void FixedUpdate()
{
Move();
Fire();
}
private void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
GameObject laser = Instantiate(myLaserPrefab1, transform.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, laserSpeed);
}
}
private void Move()
{
var deltaX = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
var deltaY = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime;
var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin + clampPadding, xMax - clampPadding);
var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin + clampPadding, yMax - clampPadding);
Vector2 movement = new Vector2(newXPos, newYPos);
//movement.Normalize();
transform.position = movement;
//myRigidbody.MovePosition(myRigidbody.position + movement);
}