Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I Don't Think I'm Using Rigidbody.AddForce Correctly. Need Some Help Making Projectile Fly Straight

Discussion in 'Scripting' started by FantasticDamage, Feb 5, 2020.

  1. FantasticDamage

    FantasticDamage

    Joined:
    May 14, 2017
    Posts:
    18
    My projectiles are not firing in a straight line. My goal is to fire projectiles at the direction that the crosshair is pointed at.

    I have a simple FPS game where the player can fire projectiles. When the user left clicks, I instantiate a projectile like this


    Code (CSharp):
    1. //spawn an iceProjectile at a Transform start position
    2. //no rotation
    3. Instantiate(iceProjectile, projectileStartPoint.transform.position, Quaternion.identity);
    Once the projectiles are instantiated, I use AddForce which takes the forward position of the player's camera and multiplies it by a simple int value (speed).

    Code (CSharp):
    1.  private void FixedUpdate()
    2. {  
    3.       Rigidbody.AddForce(PlayerController.instance.fpsCam.transform.forward * speed);
    4. }
    This is the result:


    While this opens up some interesting gameplay mechanics, curving projectiles is not what I am looking for right now. I think the reason why the projectiles are curving is because the position of the player camera is changing everytime I look around, thus, the projectile is going to head in the current position of where the camera is looking at.

    How do I get the projectiles to fire at the position of the crosshair in a straight line?
     
  2. PaulR

    PaulR

    Joined:
    Nov 14, 2012
    Posts:
    43
    You seem to be using the camera forward vector so as you rotate the camera the force vector changes.

    Try using the projectiles’ Transform.forward
     
    FantasticDamage likes this.
  3. FantasticDamage

    FantasticDamage

    Joined:
    May 14, 2017
    Posts:
    18
    I changed it to
    Code (CSharp):
    1. private void FixedUpdate()
    2. {
    3.       Rigidbody.AddForce(transform.forward * speed);
    4. }
    This is what I get:


    The projectile only fires in one direction no matter which way the player is rotated in.
     
  4. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Rotate
    Rotate the projectile to the same direction as the player when you instantiate it.
     
    FantasticDamage and PaulR like this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    Having AddForce in an Update function makes it like a rocket -- an engine, always accelerating. It's more common to give them one big force at the start, how a real bullet works.

    You may have Searched and found one way to do it. There are dozens more -- keep looking and you should find versions more like what you want, with explanations and tested code.
     
    FantasticDamage likes this.
  6. FantasticDamage

    FantasticDamage

    Joined:
    May 14, 2017
    Posts:
    18
    THANK YOU! The problem turned out to be I wasn't rotating the projectile in tandem with the first person camera. I changed the projectile instantiation to this:

    Code (CSharp):
    1. Instantiate(iceProjectile, fpsCam.transform.position, fpsCam.transform.rotation);
    Now my projectile flies straight! Thanks again guys!