Home > Software design >  Object not going the direction I am throwing it even when using transform.forward
Object not going the direction I am throwing it even when using transform.forward

Time:09-30

I want to make the grenade the player is throwing go exactly where my camera is pointed, however, if I were to face my camera lower, the grenade doesn't get thrown directly forward, as seen in the video below:

https://streamable.com/dqjq4z

And here is the function for the grenade throwing:

private void Yeet()
    {
        GameObject grenade = Instantiate(wm_equipment, FPSCamera.transform.position, Quaternion.identity);
        grenade.GetComponent<Grenade>().delay = delay;
        Rigidbody rb = grenade.GetComponent<Rigidbody>();

        Vector3 forward = FPSCamera.transform.forward;

        rb.AddForce(forward * throwForce, ForceMode.VelocityChange);
    }

I am using the forward vector of the the player's camera to tell which direction the grenade should be thrown. However it doesn't seem to be working in this case. Not sure what I've done wrong in this case, any help would be greatly appreciated.

CodePudding user response:

Grenade's collider was indeed colliding with the player's collider, hence causing it to behave in that way.

Fixed by adding a forward offset to the spawning point:

GameObject grenade = Instantiate(
    wm_equipment,
    FPSCamera.transform.position   FPSCamera.transform.forward,  // <-----
    Quaternion.identity);

Thanks to everyone who answered this question

CodePudding user response:

Not sure if this will 100% work as its from the top of my head. Sorry if its not but maybe try:

Vector3 forward = transform.position   FPSCamera.transform.forward;
  • Related