Problems with rotation script?

This script is called when the user releases the mouse button:

float rot_duration = 3f;
float rot_speed = 1.8f;
Quaternion final_rot;

void Start()
{
    cubeMesh = GameObject.FindWithTag("CubeMesh");

    Vector3 initial_rot = transform.rotation.eulerAngles;
    final_rot = Quaternion.Euler(new Vector3(initial_rot.x, initial_rot.y, 180));
}

public void Update()
{
    if (Input.GetMouseButtonUp(0)) 
    {
        StartCoroutine (DelayRotate (0.1F));
    }
}

IEnumerator DelayRotate(float waitTime)
{
        yield return new WaitForSeconds (waitTime);
        float rot_elapsedTime = 0.0F;
        while (rot_elapsedTime < rot_duration) {
        cubeMesh.transform.rotation = Quaternion.Slerp (transform.rotation, final_rot, rot_elapsedTime);
            rot_elapsedTime += Time.deltaTime * rot_speed;
            yield return null;
        }
}

This script makes the GameObject rotate, 0.1 second after releasing the mouse button. The problem is that it “flips” GameObject quickly and starts to spin.

I find it flips over final_rot2 = Quaternion.Euler(new Vector3(initial_rot.x, initial_rot.y, 180));(due to value 180) What should I do instead?

+4
source share
2 answers

I carefully studied the code and managed to detect two errors .

1. One error causing the problem:

cubeMesh.transform.rotation = Quaternion.Slerp (transform.rotation, final_rot, rot_elapsedTime);

, transform.rotation cubeMesh.transform.rotation. , . , , - GameObject while loop -, while loop. ,

Quaternion currentLocation = cubeMesh.transform.rotation;
while(...){
  cubeMesh.transform.rotation = Quaternion.Slerp (currentLocation, final_rot, rot_elapsedTime);
...Other codes
}

2. . , , , , rot_duration.

, , Quaternion.Slerp (transform.rotation, final_rot, rot_elapsedTime);.

, rot_duration , rot_elapsedTime rot_elapsedTime / rot_duration. rot_speed, , .

, , .

:

float rot_duration = 10f;
float rot_speed = 3f;
Quaternion final_rot;
GameObject cubeMesh;

void Start()
{
    cubeMesh = GameObject.FindWithTag("CubeMesh");

    Vector3 initial_rot = transform.rotation.eulerAngles;
    final_rot = Quaternion.Euler(new Vector3(initial_rot.x, initial_rot.y, 180));
}

public void Update()
{
    if (Input.GetMouseButtonUp(0))
    {
        StartCoroutine(Delay(1));
    }
}

IEnumerator Delay(float waitTime)
{
    yield return new WaitForSeconds(waitTime);
    float rot_elapsedTime = 0.0F;

    //Get the current rotation 
    Quaternion currentLocation = cubeMesh.transform.rotation;

    while (rot_elapsedTime < rot_duration)
    {
        rot_elapsedTime += Time.deltaTime;
        cubeMesh.transform.rotation = Quaternion.Slerp(currentLocation, final_rot, rot_elapsedTime / rot_duration);
        yield return null;
    }
}
+1

, . , Time.deltaTime, , , .

:

float rot_duration = 10f;
float rot_speed = 3f;
float rot_elapsedTime = 3f;
Quaternion final_rot;

public void Update()
{
    if (Input.GetMouseButtonUp(0)) 
    {
        StartCoroutine (Delay (1));
    }
    if (rot_elapsedTime < rot_duration) {
        cubeMesh.transform.rotation = Quaternion.Slerp (transform.rotation, final_rot, rot_elapsedTime);
        rot_elapsedTime += Time.deltaTime * rot_speed;
}

IEnumerator Delay(float waitTime)
{
   yield return new WaitForSeconds (waitTime);
   rot_elapsedTime = 0.0F;
}

, , transform.Rotate, , .

: OP , 1 .

0

All Articles