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

Question Addforce To an object forward a Player

Discussion in 'Scripting' started by Busiplutarco, Oct 17, 2022.

  1. Busiplutarco

    Busiplutarco

    Joined:
    Oct 17, 2022
    Posts:
    2
    Hi. I have an issue trying to make an AddForce to a object in Unity3D. I want that object to be pushed in front of my character, but the AddForce is allways applied in the same direction, not in the Player.Forward position. Heres the part of the code :

    Code (CSharp):
    1. public Transform Player;
    2. private Rigidbody rigidboxx;
    3.  
    4. void Update(){
    5. if (Input.GetKeyDown("g") && (objactual.tag == "Grabbable"))
    6.         {
    7.             rigidboxx.AddForce(Player.forward * forceV);
    8.            
    9.         }
    10. }
     
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    76
    This code should work. Make sure you are assigning the player object to the Player field instead of something else.
     
    Busiplutarco likes this.
  3. Busiplutarco

    Busiplutarco

    Joined:
    Oct 17, 2022
    Posts:
    2
    Everything seems to be ok, but still not working, the objects just going in another direction
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    A small snippet of code isn't a way to figure out what is going wrong. You need to debug it.

    You can verify what "Player.forward" is. You can verify what "forceV" is and what the result is when multiplied. You can stop adding the force to see if it stops moving and it's not something else moving it such as collision or another script etc.

    Adding a single force might not do much, the force you specify will be time-integrated. A one-off "force" is typically done as an impulse which you can specify as the second argument to "AddForce".
     
    Busiplutarco likes this.