Search Unity

Question Shooting using AddRelativeForce does not follow forward vector of gun

Discussion in 'VR' started by Deleted User, Mar 3, 2021.

  1. Deleted User

    Deleted User

    Guest

    Hi all,

    I am currently trying to develop a game for a school assignment, on Oculus Quest 1.

    Right now, the shooting mechanism is to grab and object then shoot the object based on the foward vector of the gun. I have a line renderer that renders the forward vector of the gun.

    However, whenever I try to shoot the projectile, unless the gun is facing straight forward, the projectile will fly at a weird angle that does not follow the forward vector.

    Here is my code currently for shooting

    Code (CSharp):
    1. void ShootObject() {
    2.         projectile.transform.parent = null;
    3.         projectile.GetComponent<Rigidbody>().isKinematic = false;
    4.  
    5.         projectile.GetComponent<Rigidbody>().AddRelativeForce(transform.forward * bulletLaunchForce, ForceMode.Impulse);
    6.         // There is a seperate script controlling the projectile, that does not change the movement of the projectile at all
    7.         projectile.GetComponent<ProjectileController>().ToggleShotState();
    8.  
    9.         isArmed = false;
    10.         projectile = null;
    11. }
    I have no idea why this doesn't work as the line renderer using transform.forward clearly points in the right direction, but using transform.forward here does not make the projectile shoot in the right direction.

    Is it because I'm using ForceMode.Impulse?