var speed : float = 5;
var torsoRotation : float = 0;
function Update(){
torsoRotation = transform.Rotate(0, Input.GetAxis("Mouse X") * speed, 0, Space.World);
Mathf.Clamp(torsoRotation, -30, 30);
}
This is the error I get in Unity:
Assets/Scripts/mouseRotate.js(5,35): BCE0022: Cannot convert 'void' to 'float'.
I have a full player model. I was trying to get the torso to rotate slightly left and right, as I moved the mouse those directions in first-person. (Just a little bit of realism.) It worked! I added a variable to store the rotation in, and then I clamped it, so it would not rotate infinitely. Then, it did not work.
Of course, my logic is probably completely wrong. Is anyone able to explain what I did wrong? Am I unable to store the rotation?
transform.Rotate does not return a value, so assigning it to a float doesn’t make any sense. Calculate the value you want to rotate by, clamp it, and then rotate the object.
Try this:
var speed : float = 5;
var torsoRotation : float = 0;
function Update() {
torsoRotation = Input.GetAxis("Mouse X") * speed;
Mathf.Clamp(torsoRotation, -30, 30);
transform.Rotate(0, torsoRotation, 0, Space.World);
}