Friday, August 24, 2018

c# - How to rotate within a fixed interval in Unity?


I have very recently started to learn Unity.


I'm trying to write code that will make a character swing a flag. I want to use this object and rotate the arm up and down. So I need to make it rotate up and down with within 30 degrees.



This is my attempt but it simply keeps on turning. How would I make the hand rotate in motion within the interval [-30, 30] degrees.


private GameObject myobject;
public float timeElapsed =0;
public float delay = 3.0f;
float OO = 0.0f;

void Start () {
myobject = GameObject.Find ("rightarm").gameObject;
}


void Update () {
timeElapsed += Time.deltaTime;
Debug.Log (timeElapsed);

if (timeElapsed >= delay) {
OO = 30.0f;
timeElapsed = 0;
if (myobject.renderer.enabled == false) {
myobject.renderer.enabled = true;
}


myobject.transform.Rotate (OO, 0.0f, 0.0f);
}
else
{
myobject.transform.Rotate (-OO,0.0f, 0.0f );
}
}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...