Quantcast
Channel: Answers by "Flynn"
Viewing all articles
Browse latest Browse all 59

Answer by Flynn

$
0
0
Mathematically calculate your rotation based on speed: float rotX = (verticalSpeed / maximumVerticalSpeed) * 45 IF the object is traveling downwards at maximum speed, this will return -45. It will return 45 for upwards travel. If it is moving up at half maximum vertical speed, it will return 22. EDIT: Took me a bit, but I have some test code that seems to do what you want it to do. using UnityEngine; using System.Collections; public class TiltingGameObject : MonoBehaviour { Vector3 lastPos; public float targetH = 20; public float maxDist = 20; public float maxAng = 45; public float rotSpeed = 10; void Start() { lastPos = transform.position; } void Update() { transform.Translate(0, 0 , 1); Vector3 diff = transform.position - lastPos; if (Mathf.Abs(transform.position.y - targetH) > 0.1f) { float rotX = Mathf.Abs(Mathf.Abs(transform.position.y-targetH)/maxDist) * (transform.position.y > targetH ? -1 : 1) * -maxAng; if (rotX > maxAng) rotX = maxAng; if (rotX < -maxAng) rotX = -maxAng; transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(rotX, 0, 0)), Time.deltaTime * rotSpeed); } lastPos = transform.position; } }

Viewing all articles
Browse latest Browse all 59

Trending Articles



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