Question by
pohshania · Aug 14, 2017 at 05:57 PM ·
colliderrigidbody2dvelocity2d-physics
Help! My projectile velocity is not working..
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!
Comment
Answer by unidad2pete · Aug 14, 2017 at 06:06 PM
Probably, your proyectile are Triggered by your player collider or other collider, find out.
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag != "TAG OF YOUR PLAYER OR COLLIDER CAUSING THIS TRIGGER")
{
Destroy(gameObject);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag != "Player")
Destroy(gameObject);
}