Quantcast
Channel: Questions in topic: "quaternion.slerp"
Viewing all articles
Browse latest Browse all 133

Transform rotation and position on key input: rotation only working the first time

$
0
0
Hi, I want to rotate and move a game object to a set position and rotation. The transform.position is working fine, but the rotation only works the first time I press the q and w key, but after that it doesn't rotate the object to the correct position. I just can't work out what I'm doing wrong - I thought maybe I wasn't resetting a value correctly, but I can't see what I'm missing. So I want the object to rotate to 9 degrees on the x axis every time q is pressed, and 45 degrees on the x axis when w is pressed. Does anyone have any ideas on what I'm doing wrong? Best, Laurien Edit: updated script using UnityEngine; using System.Collections; public class MovementRotationCoroutine : MonoBehaviour { public GameObject cameraMain; private float degree; private float angle; void Update () { if (Input.GetKeyDown("q")) { degree = 9f; StartCoroutine ( MoveToPositionPersp (new Vector3(0,0.5f,-5.5f), 2f, 2f)); } if (Input.GetKeyDown("w")) { degree = 45f; StartCoroutine ( MoveToPositionOrtho (new Vector3(0,5.5f,0), 2f, 2f)); } } public IEnumerator MoveToPositionOrtho(Vector3 position, float timeToMove, float waitTime) { yield return new WaitForSeconds(waitTime); var currentPos = transform.position; var currentPosRotate = transform.rotation; var t = 0f; while(t < 1) { t += Time.deltaTime / timeToMove; transform.position = Vector3.Lerp(currentPos, position, t); angle = Mathf.LerpAngle(transform.rotation.x, degree, t); transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t); yield return null; } } public IEnumerator MoveToPositionPersp(Vector3 position, float timeToMove, float waitTime) { yield return new WaitForSeconds(waitTime); var currentPos = transform.position; var currentPosRotate = transform.rotation; var t = 0f; while(t < 1) { t += Time.deltaTime / timeToMove; transform.position = Vector3.Lerp(currentPos, position, t); angle = Mathf.LerpAngle(transform.rotation.x, degree, t); transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t); yield return null; } } }

Viewing all articles
Browse latest Browse all 133

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>