- Home /
The question is answered, right answer was accepted
Consider storing the value in a temporary variable Error
using UnityEngine;
using System.Collections;
public class YSpeed : MonoBehaviour {
KeyCode moveUp;
KeyCode moveDown;
float speed = 10;
void Update (){
if (CFInput.GetKey(moveUp))
{
rigidbody.velocity.y = speed;
}
else if (CFInput.GetKey(moveDown))
{
rigidbody.velocity.y = speed *-1;
}
else
{
rigidbody.velocity.y = 0;
}
}
}
it's says 'Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable.'
It was Javascript at the first and im trying to convert to C#.
Answer by khenkel · Sep 09, 2014 at 02:04 PM
You need to modify the entire velocity variable. x, y and z are read-only. Nonetheless it is not recommended to modify the velocity variable. You should use Physics stuff instead, like AddForce().
http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
rigidbody.velocity = new Vector3(rigidbody.velocity.x, speed, rigidbody.velocity.z);
Follow this Question
Related Questions
Converting vector3 velocity to an angel 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Finding plane perpendicular to an objects velocity 1 Answer
Physics: Auto-stopping a spaceship with reverse thrusters 1 Answer
My character is falling and standing/jumping at the same time? 0 Answers