Search Unity

[Newbie/C#] Can't change velocity of a 2D rigidbody

Discussion in 'Scripting' started by Edern, Mar 25, 2015.

  1. Edern

    Edern

    Joined:
    Mar 25, 2015
    Posts:
    3
    Hello !
    I'm new with Unity, and I decided to go through this course, after completing the "Roll-a-Ball" one. The problem is, I don't know Javascript, so I decided to convert that guy's code to C#. After some fiddling, I got the code to compile, but when I press the key assigned to "moveUp" or "moveDown" during play mode, nothing happens. In the console, I've got this warning : Assets/PlaerController.cs(11,23): warning CS0414: The private field `PlaerController.velocity' is assigned but its value is never used. Here's the code :
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlaerController : MonoBehaviour
    6. {
    7.  
    8.     public KeyCode moveUp;
    9.     public KeyCode moveDown;
    10.     public float speed = 10;
    11.     private Rigidbody2D pong;
    12.     private float velocity;
    13.  
    14.     void Start ()
    15.     {
    16.         pong = gameObject.GetComponent<Rigidbody2D>();
    17.         velocity = pong.velocity.y;
    18.  
    19.     }
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.  
    24.  
    25.    
    26.         if (Input.GetKey(moveUp))
    27.         {
    28.             velocity = speed;
    29.         }
    30.         else if (Input.GetKey(moveDown))
    31.         {
    32.             velocity = speed * -1;
    33.         }
    34.         else
    35.         {
    36.             velocity = 0;
    37.         }
    38.  
    39.     }
    40. }
    The "pong" variable refers to the rigidbody of the rectangle you control (not native English speaker, don't know how it is called). For those who can't see the video, the purpose is to make a pong-clone game, and this script is the player controller.
    Hope my English is understandable, I'm dead tired right now so I probably made a lot of mistakes.
    Thanks by advance.
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Nothing happens when you press the keys because your not changing anything expect the velocity float variable. When I take a look at the tutorial, it looks like he is changing the rigidbody2d.velocity.y value. So something more like this (I have not tested the code, but it should give you an idea).

    Code (CSharp):
    1.   if (Input.GetKey(moveUp))
    2.         {
    3.                 pong.velocity.y = speed;
    4.         }
    5.         else if (Input.GetKey(moveDown))
    6.         {
    7.           pong.velocity.y = speed * -1;
    8.         }
    9.         else
    10.         {
    11.             velocity = 0;
    12.         }
     
  3. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    roger0 was quicker ;)

    It looks like your are trying to create a reference to pong.velocity.y at line 17. In fact you are just assigning the float value to the velocity variable.
    When you change this variable later it has no "connection" on pong.velocity.y


    Roger0 has given you the solution. (which is exactly what was in the tutorial as well..)
     
  4. Edern

    Edern

    Joined:
    Mar 25, 2015
    Posts:
    3
    Thanks for your replies, but when I try with this code it returns me the following error : Assets/PlaerController.cs(26,30): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity'. Consider storing the value in a temporary variable. (same at line 30)
     
  5. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    It only lets you modify a value if you modify the whole value, not just one axis. You have to modify all three axis in order for it to work.

    Here's more info about the error.

    http://answers.unity3d.com/questions/496341/consider-storing-the-value-in-a-temporary-variable.html

    Somthing like this. (Untested code)

    Code (CSharp):
    1.       if (Input.GetKey(moveUp))
    2.             {
    3.                
    4. Rigidbody2d tempVelocity;
    5. tempVelocity.velocity = pong.velocity;
    6.  
    7. tempVelocity.y = speed;
    8.  
    9. pong.velocity] = tempVelocity
    10.             }
    11.             else if (Input.GetKey(moveDown))
    12.             {
    13.  
    14. Rigidbody2d tempVelocity;
    15. tempVelocity.velocity = pong.velocity;
    16.  
    17. tempVelocity.y = speed * -1;
    18.  
    19. pong.velocity = tempVelocity
    20.  
    21.  
    22.          
    23.             }
    24.             else
    25.             {
    26.                 velocity = 0;
    27.             }
    28.  
     
  6. Edern

    Edern

    Joined:
    Mar 25, 2015
    Posts:
    3
    Thanks a lot ! Gonna try this tomorrow :)

    EDIT : Working, thanks ! Just had to change some little things. Here's the final code for those interested :
    Code (csharp):
    1.  
    2. if (Input.GetKey(moveUp))
    3.         {
    4.             Vector2 tempVelocity;
    5.             tempVelocity = pong.velocity;
    6.             tempVelocity.y = speed;
    7.             pong.velocity = tempVelocity;
    8.             //Repeat for the two next else blocks, just assign tempVelocity.y to speed * -1 then to 0.
    9.  
    It could be better to put that in a method, but I should be able to do it. Thanks again !
     
    Last edited: Mar 27, 2015