Hi there. I am making a 2D platformer game and I can’t solve why my projectile doesn’t move when instantiated. It is stuck at the player’s hand and immediately disappear.
In my player controller script:
// projectile
public Transform firePoint;
public GameObject projectile
if(Input.GetKeyDown(KeyCode.X))
{
Instantiate(projectile, firePoint.position, firePoint.rotation);
}
FirePoint is the position of the player’s hand and Projectile is my projectile gameobject.
And I attach this script to my projectile:
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>
().velocity.y);
}
void OnTriggerEnter2D(Collider2D other)
{
Destroy(gameObject);
}
When I press X,
The projectile just appear and disappear immediately, but doesn’t transform to right.
However when I put one of the projectile by itself in the scene, it works perfectly fine. The projectile will move towards right.
Why is that so??? Thanks for help!