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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Input.GetAxis tearing my hair out

Discussion in 'Scripting' started by Unlimited_Energy, Jun 4, 2015.

  1. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovementTWO : MonoBehaviour
    5. {
    6.     public Rigidbody rb;
    7.  
    8.     void Start() {
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.     void FixedUpdate() {
    12.         if (Input.GetAxis ("Vertical")!= 0)
    13.             rb.AddForce (0, 0, 1);
    14.      
    15.     }
    16. }
    Hello everyone,

    I am trying to move my main character which is a non kinematic rigidbody, In the vertical axis,. My character only moves in one direction, when I push up on the joystick the character moves but when I pull down on the joystick in the opposite direction nothing happens.. If i remove the !=0 from the line of code if (Input.GetAxis ("Vertical")!= 0) then I get this error: "if error CS0029: Cannot implicitly convert type `float' to `bool'"

    EDIT: it seems that only rb.velocity = new Vector3(0, 0, 1); moves my character (not the way I want moved but atleast something happens when i move the joystick) at all and not rb.AddForce (0, 0, 1); which to me does not make any sence since AddForce is for moving objects with force every frame and Velocity is for objects which have a big starting power then getting slower by drag.
     
    Last edited: Jun 4, 2015
  2. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Please use code tags when posting scripts..

    I copy/pasted your script into unity and it appears to be working fine.

    Perhaps you forgot to add a RigidBody to your GameObject? Or perhaps a force of 1 simply isn't enough to visibly move your object.
     
    Unlimited_Energy likes this.
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    A force of 1 acting on an rb with a mass of 10000 is literally nothing.
    Add more force, or give your object less mass
     
    Unlimited_Energy likes this.
  4. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Inspector.png Its still not working. The character just keeps moving non stop when I hit up on the joystick. the force is constant. I thought addforce was only suppose to add force when the joystick is up or down.... Is it because I have !=0 after add force which means the jopysticks value is never 0? If I hit down or up from the very begining before any player movement happens, the character moves in the same direction no matter up or down. I need the characters movement to reverse the direction on joystick being pulled down. I thought that was to happen automatically.....

    EDIT:
    rb.velocity = new Vector3(0, 10, 0); in place of rb.AddForce (0, 0, 10); does THE EXACT SAME THING.
     
    Last edited: Jun 4, 2015
  5. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    UPDATE: By adding else rb.velocity = new Vector3(0, 0, 0) it seems to have stopped my character from constantly moving, however, pushing down on the joystick still causes the player to move in the same direction as up on the joystick.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovementTWO : MonoBehaviour
    5. {
    6.     public Rigidbody rb;
    7.  
    8.     void Start() {
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.     void FixedUpdate() {
    12.         if (Input.GetAxis ("Vertical")!= 0)
    13.             rb.velocity = new Vector3(0, 0, 10);
    14.         else
    15.             rb.velocity = new Vector3(0, 0, 0);
    16.     }
    17.  
    18. }
     
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I think the normal way to get something to stop moving is to adjust its drag properties or the friction of the surface along which it is traveling. If the object is not stopping quick enough, crank up the friction.

    The reason your object only moves forwards is because you always add a positive force / velocity. Input.GetAxis will return a float between -1 and 1, so you could multiply your vector by the value of Input.GetAxis. And if you're going to do that, you don't really need the if statement either..
     
    Unlimited_Energy likes this.
  7. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Awsome, thanks for the help. Do you think you could give me an example code of the proper way to multiply my vector byt he value of Input.GetAxis. I'm very new to coding and I'm not familiar enough yet to understand how to structure what you just told me to do :(
     
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I would try something like this.. but note that I'm a novice with Unity too.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovementTWO : MonoBehaviour
    5. {
    6.   Rigidbody rb;
    7.   public float thrust = 5f;
    8.  
    9.   void Start() {
    10.     rb = GetComponent<Rigidbody>();
    11.   }
    12.   void FixedUpdate() {
    13.     rb.AddForce (Input.GetAxis ("Horizontal") * thrust, 0, Input.GetAxis ("Vertical") * thrust);
    14.   }
    15. }
     
    Unlimited_Energy likes this.
  9. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I don't think that you should use "GetAxis" under FixedUpdate, it does not run every frame, and as such, might miss a few inputs.
    If you want to use the AddForce inside FixedUpdate, get the inputs in normal update and use them in FixedUpdate.

    Something like below?
    Note that I am not near my Unity machine, so I can't verify my code, and I don't remember things so well by myself (I fail at theoretical classes, but ace my practical ones).
    I don't know how good it is to create a new Vector3 every fixed frame though.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class MovementTWO : MonoBehaviour
    4. {
    5.     Rigidbody rb;
    6.     public float thrust = 5f;
    7.     private float horizontal;
    8.     private float vertical;
    9.    
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         horizontal = Input.GetAxis ("Horizontal");
    18.         vertical = Input.GetAxis ("Vertical");
    19.     }
    20.    
    21.     void FixedUpdate()
    22.     {
    23.         rb.AddForce (new Vector3(horizontal, 0, vertical) * thrust);
    24.     }
    25. }
    26.  
     
    Last edited: Jun 4, 2015
    Unlimited_Energy likes this.
  10. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Thanks man. It works if I set the thrust variable to 1000 and the mass to 1 but even then its extremely slow to start moving and stop moving and the motion at its fastest is barley moving. Its like extreme slow motion.
     
  11. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    I have "vertical" mapped to the 360 controllers y axis and "horizontal" mapped to 4th axis. I know the issue in my code below, horizontal if equal to zero sets vector movement to 0 which conflicts with my vertical movement. If Im moving my vertical joystick up and my 4th axis horizontal is at 0 they are fighting each other for what to set the vector movement too. Any idea how I can fix this?



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveUpdate : MonoBehaviour {
    5.     public Rigidbody rb;
    6.  
    7.     void Start() {
    8.         rb = GetComponent<Rigidbody>();
    9.     }
    10.     void Update()
    11.     {
    12.         if (Input.GetAxis ("Vertical") > 0)
    13.             rb.velocity = new Vector3 (0, 0, 30);
    14.  
    15.         if (Input.GetAxis ("Vertical") < 0)
    16.             rb.velocity = new Vector3 (0, 0, -30);
    17.  
    18.         if (Input.GetAxis ("Vertical") == 0)
    19.             rb.velocity = new Vector3 (0, 0, 0);
    20.  
    21.         if (Input.GetAxis ("Horizontal")> 0)
    22.             rb.velocity = new Vector3 (0, 0, -30);
    23.      
    24.         if (Input.GetAxis ("Horizontal")< 0)
    25.             rb.velocity = new Vector3 (0, 0, 30);
    26.      
    27.         if (Input.GetAxis ("Horizontal") == 0)
    28.             rb.velocity = new Vector3 (0, 0, 0);
    29.     }
    30.  
    31.  
    32.  
    33. }
     
    Last edited: Jun 5, 2015
  12. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Sorry, I don't know what "4th axis" means.
    Why do you only change the z component of your velocity? Don't you want to set the x component based on your Horizontal axis?
    Also why hard coded 30? Do you not like the analogue effect? If you really want to modify velocity directly instead of using forces, I would still do something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovementTWO : MonoBehaviour
    5. {
    6.   Rigidbody rb;
    7.   public float thrust = 5f;
    8.  
    9.   void Start() {
    10.     rb = GetComponent<Rigidbody>();
    11.   }
    12.   void FixedUpdate() {
    13.     rb.velocity += (Input.GetAxis ("Horizontal") * thrust, 0, Input.GetAxis ("Vertical") * thrust);
    14.     rb.velocity = Vector3.ClampMagnitude(rb.velocity, 30f);
    15.   }
    16. }
    If you're not happy with the way the physics is working, you should start a new thread to get advice on what physics settings you need to add to your scene. Perhaps in the Physics forum.
     
    Unlimited_Energy likes this.
  13. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
  14. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Just throwing AddRelativeForce out there in case ya need it.
    Also, purhaps you can do this to avoid conflict.
    Code (CSharp):
    1. if (Input.GetAxis ("Vertical") == 0 && Input.GetAxis ("Horizontal") == 0)
    2.             rb.velocity = new Vector3 (0, 0, 0);
    3.  
     
    Unlimited_Energy likes this.
  15. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Thanks man that worked perfect.
     
  16. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469

    What does the ClampMagnitude do in that line of code? Im trying to understand coding more. I looked up the definition for clamp magnitude, but I do not understand exactly what it does in relation to that line of code you included it in.
     
  17. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    It seems like he is just using it to limit how much velocity you can give your rigidbody. The magnitude of a vector is like a single number representing the vectors overall power, by using clamp magnitude, you give it your current vector and then a number representing the highest magnitude you'll allow the vector to have.
    He did rigidbody.velocity +=
    If he didn't clamp the rigidbodies velocity, then the rigid body would just keep going faster and faster.
     
    Unlimited_Energy and eisenpony like this.
  18. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    HiddenMonk is correct.

    Think of a vector like the hypotenuse of a right angled triangle. Each triangle can have a width and a height. The height of the triangle has no bearing on the width of the triangle and vice versa. However, both height and width have an effect on the hypotenuse. The hypotenuse therefore has three components, the width (x) component, the height (y) component and the overall length component (magnitude). If we set the vector to have a width of 30 and a height of 0, then the total length of the vector would be 30. However, If we set both the width and height to 30, we use Pythagoras theorem to calculate the total length (30^2 + 30^2) ^ 0.5 ~= 42.4; much longer than the length of its components.

    This is true of vectors as well, though for 3 dimensional vectors, we add another component: depth (z). When we say magnitude of a vector, we are talking about the overall length. When I used ClampMagnitude, I was preventing the hypotenuse from growing larger than 30 units. If it ever does increase beyond 30, then its height, width and depth components will be reduced as needed.
     
    Unlimited_Energy likes this.
  19. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    ahhh great info to know. So you could use ClampMagnitude on a object that has a velocity that, lets say increases its velocity on every collision. I could use clamp magnitude to limit how much velocity or speed that object can max out at?
     
  20. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Yes, but keep in mind that when altering velocity directly such as using an = sign, might cause problems. Any other important forces added to your rigidbody, such as forces that push your rigidbody out of colliders on collision, might be overwritten by your new assigned velocity.
    I am not completely sure, but I think I am correct.
     
    Unlimited_Energy likes this.