For Rotation on Z-Axis

Asked

Viewed 1,136 times

3

I’m new to the c# code and I’m doing a basic Z-axis rotation in Unity 3d, but I’m having a problem to stop rotating. The initial code is this:

using UnityEngine;
using Sysstem.Collections;

public class voa : MonoBehaviour{

int vel = 150;

// Use this for initialization
void Start (){

}

// Update is called once per frame

void Update () {

if (Input.GetButton("right)){

    transform.Rotate(0,0,vel * Time.deltaTime);

    }

    if (INput.GetButton ("left)){

    transform.Rotate (0,0,-vel * Time.deltaTime);

    }

  }

}

What would be the sequence of this code, so that when the object is 80 degree (positive or negative), even pressing the arrow button it does not pass this point.

  • 5

    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].

1 answer

5


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.

  • 1

    Thank you very much for the answer worked, but there was a doubt! If the object is tilted at the angle of 90 degree, you have the possibility of it rotating in Y. Ex:An Airplane when you turn it leans to the side and then turns, would you be able to do this movement by pressing a button? it rotated on the z axis and turned on the y axis on both sides.

  • Not at all. Well, I didn’t get it right. You mean rotating on another axis from the moment the first axis has reached its maximum limit? If yes, of course there is. Just use two variables and do two checks (one inside the other). Anyway, maybe you should open a new question. Remember: this site is not a forum, but a question and answer site. :)

  • Ah, and if the answer was helpful and helped you, please consider marking it as accepted: http://meta.pt.stackoverflow.com/questions/1078/como-e-por-que-accepta reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.