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. Dismiss Notice

Addforce based on player direction

Discussion in 'Scripting' started by Emolk, May 14, 2014.

  1. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    This is what i have currently:

    Code (csharp):
    1. #pragma strict
    2.  
    3. var fireball : Transform;
    4. var mana : int = 10;
    5. var moveRight : KeyCode; // Set in Inspector
    6. var moveLeft : KeyCode;
    7.  
    8.  
    9. function Start () {
    10.  
    11. }
    12.  
    13. function Update () {
    14.  
    15. if(Input.GetKeyDown("space")  (Input.GetKey("d"))){
    16. Instantiate(fireball, transform.position, transform.rotation); // Create object at player's position (relative)
    17. rigidbody2D.AddForce (Vector3.right * 8);
    18. }
    19.  
    20. if(Input.GetKeyDown("space")  (Input.GetKey("a"))){
    21. Instantiate(fireball, transform.position, transform.rotation); // Create object at player's position (relative)
    22. rigidbody2D.AddForce (Vector3.left * 8);
    23. }
    24.  
    25.  
    26. }

    But with this script, the fireballs then spawn and change direction if i change direction. Eg, if i shoot one to the right, it moves right, but if i then press a to move my player to the left, the same fireball changes direction and heads to the left.

    How do i stop the fireball changing direction after it has picked which direction to head to?
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    You can try to use transform.right instead of Vector3.right:
    Code (csharp):
    1.  
    2. rigidbody2D.AddForce (transform.right * 8)
    3. ///instead of
    4. rigidbody2D.AddForce (Vector3.right * 8);
    5.  
     
    Last edited: May 14, 2014
  3. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Error: 'left' is not a member of 'UnityEngine.Transform'.
     
  4. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    left is -right.
     
  5. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Code (csharp):
    1. rigidbody2D.AddForce (transform.-right * 8);
    This gives me: Expecting ), found "."
     
  6. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    my bad, i was not clear enough...
    Code (csharp):
    1.  
    2. rigidbody2D.AddForce (-transform.right * 8);
    3. //or
    4. //rigidbody2D.AddForce (transform.right * -8);
    5.  
    when I said that left is -right i was referring to Vector3, so transform.right = right and the opposite of that -transform.right is "transform's left".
     
  7. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Oh okay. It still does the same thing as my original post though. Literally nothing changed.
     
  8. ChechW

    ChechW

    Joined:
    Dec 17, 2013
    Posts:
    2
    Every fireball you instantiate will change it's force every Update depending on your keypresses (and each one should also be spawning one new fireball).
    You should have one instance where you check for keypresses and instantiate the fireballs (in player script or a new one maybe), then instantiate a new fireball and either use ConstantForce (https://docs.unity3d.com/Documentation/ScriptReference/ConstantForce.html) or have AddForce in fireball Update and have it move depending on the direction you assign it when you instantiate the fireball.