Search Unity

Rigidbody2D.velocity Won't Move Object

Discussion in '2D' started by Tamariniak, Feb 11, 2017.

  1. Tamariniak

    Tamariniak

    Joined:
    Feb 11, 2017
    Posts:
    27
    This is driving me crazy. I have this simple player controller and the animator works as intended but none of the Rigidbody.velocity lines seem to do anything.
    Code (csharp):
    1.     public float Speed = 10f;
    2.  
    3.     public Rigidbody2D Rigidbody;
    4.     private Animator Animator;
    5.  
    6.     void Start ()
    7.     {
    8.         Rigidbody = GetComponent<Rigidbody2D>();
    9.         Animator = GetComponent<Animator>();
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.  
    15.         if (Input.GetAxis("Vertical") > 0f)
    16.         {
    17.             Animator.SetTrigger("WalkUp");
    18.             Rigidbody.velocity = new Vector2(0f, Speed);
    19.         }
    20.  
    21.         if (Input.GetAxis("Vertical") < 0f)
    22.         {
    23.             Animator.SetTrigger("WalkDown");
    24.             Rigidbody.velocity = new Vector2(0f, -Speed);
    25.         }
    26.  
    27.         if (Input.GetAxis("Vertical") == 0f)
    28.         {
    29.             Animator.SetTrigger("WalkForward");
    30.             Rigidbody.velocity = Vector2.zero;
    31.         }
    32.     }
    I've tried GetAxisRaw, I've tried putting in '0' instead of '0f', I've tried using FixedUpdate. I've already had a really similar thing working in Unity 5.3 with Input.GetKey. Not even using that seems to work here, in Unity 5.5.
    Also sorry for showing you this much code, but I felt like I had to because I don't really know where the mistake is.
     
  2. diegzumillo

    diegzumillo

    Joined:
    Jul 26, 2010
    Posts:
    416
    I could be way off here, but if this is c# I think there are a few syntax problem in your variable definitions. For example, line x should be something like
    Code (CSharp):
    1. Rigidbody2D rb = GetComponent<Rigidbody2D>();
    and then be accessed like rb.velocity etc.

    Again, I could be completely off! If my thinking is correct your couldn't shouldn't even compile though, which doesn't seem to be the case.
     
    vakabaka likes this.
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I think, Rigidbody is the class, you cannot use this word as referenced variable. Do as diegzumillo said, "rb" is ok
    public Rigidbody2D rb;
     
  4. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    82
    Is it kinematic?
     
unityunity