Search Unity

"Expressions In Statements Must Only Be Executed For Their Side-Affects"

Discussion in 'Scripting' started by Foofy69, Jan 7, 2014.

  1. Foofy69

    Foofy69

    Joined:
    Oct 13, 2013
    Posts:
    43
    Now first off, I'm new, this is my second day. Now I read what this means and I know exactly what this means. The problem is though, I am following this tutorial and I SWEAR I am doing exactly what I am supposed to do, and typing exactly what I'm supposed to type. Please help, this is extremely aggrivating. AND ALSO, I made sure that the parents and children are just the way he did it, attached everything to the same thing he did, etc. And if I remove this line of code, everything on the map turns glitchy and it's like an earthquake happening and makes everything difficult to control. I am making a script for walking, and what I am doing is making a script where there is a "max walk speed", which is what I'm stuck on. Thanks! (And it say's "11,20" for what line the error is on).

    Code (csharp):
    1. var walkAcceleration : float = 10;
    2. var cameraObject : GameObject;
    3. var maxWalkSpeed : float = 20;
    4. var horizontalMovement : Vector2;
    5.  
    6. function Update () {
    7.  
    8. horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    9. if(horizontalMovement.magnitude > maxWalkSpeed)
    10. {
    11. horizontalMovement.Normalize;
    12. horizontalMovement *= maxWalkSpeed;
    13. }
    14.  
    15. rigidbody.velocity.x = horizontalMovement.x;
    16. rigidbody.velocity.z = horizontalMovement.y;
    17.  
    18. transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    19. rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
    20. }
     
    Last edited: Jan 7, 2014
  2. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Try: horizontalMovement.Normalize();

    (been a while since I used unityscript so may not be the issue... but try it :) )
     
  3. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    I would have thought you would need something like this
    Code (csharp):
    1. horizontalMovement = horizontalMovement.Normalize*maxWalkSpeed;
    sorry I was thinking of normalized

    Where is this tutorial
     
    Last edited: Jan 7, 2014
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    +1. Normalize is a function so use the () to execute it.
     
  5. Foofy69

    Foofy69

    Joined:
    Oct 13, 2013
    Posts:
    43
    Last edited: Jan 7, 2014
  6. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    I was almost correct the first time, in the video around 14.28 he changes it:

    line 11 should be
    Code (csharp):
    1. horizontalMovement = horizontalMovement.normalized;
    you might be able to combine line 11 and 12 into
    Code (csharp):
    1. horizontalMovement = horizontalMovement.normalized*maxWalkSpeed;
     
    Last edited: Jan 7, 2014