I am trying to make a top down game and I have a projectile firing from the player however I cant seem to get it to work correctly. It works only some of the time… If I move around and shoot it sometimes goes different directions. Here is my Scipt:
using UnityEngine;
using System.Collections;
public class BouncyBall : MonoBehaviour {
public Rigidbody BouncyBallPrefab;
private Transform ballTransform;
public float Speed;
void Start()
{
ballTransform = transform;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("left ctrl"))
{
Rigidbody instantiatedBouncyBallPrefab = (Rigidbody)Instantiate(BouncyBallPrefab, transform.position, transform.rotation);
instantiatedBouncyBallPrefab.velocity = new Vector3(transform.position.x, transform.position.y, Speed) ;
}
}
}
When you’re setting the velocity of the ball, why are you using the transform coordinates for the x and y components? For a projectile’s velocity, if you only want it moving forward, you would only need to set the z component. x and y values are going to interfere with that.