Your code had a few typos and it didn’t work here directly. Well, having them fixed, one idea that might help you is to simply limit the rotation before you run it. To do this, accumulate the value of the rotation to each frame in which this occurs and compare if the accumulated value exceeds (both in the positive "direction" and in the negative direction) its maximum rotation. The following code does this:
using UnityEngine;
using System.Collections;
public class voa : MonoBehaviour{
int vel = 150;
float acumulado = 0;
public float limite_angular = 90;
// Use this for initialization
void Start (){
}
// Update is called once per frame
void Update () {
if(Input.GetButton ("Horizontal")) {
float angulo = vel * Time.deltaTime * Input.GetAxisRaw("Horizontal");
if( (acumulado + angulo) >= limite_angular || (acumulado + angulo) <= -limite_angular)
angulo = 0;
acumulado += angulo;
transform.Rotate (0, 0, angulo);
}
}
}
Note that I am just making this accumulation on the Z axis (which is what you use in your original code). If you need to accumulate in the three axes, change the variable of float
for Vector3
and accumulate in each of them. Note also that I am using the variable "horizontal" instead of "right" and "left" (because that is the standard of Unity - at least in the latest version). And finally, note also, that to have the sign (positive or negative) just multiply by GetAxisRaw
(function that returns -1 or 1 to indicate the direction of movement on the horizontal axis).
Ah, I put the variable limite_angular
as public, so you can change its value directly in the Inspector.
Dude, put in the code and not a picture of it. There’s a lot of people who can’t see the images because of blockages. Visit [tour], see [help] and [Ask]. This will help you improve your question. (Whenever you want you can edit it by clicking [Edit].
– Jéf Bueno