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;
}
}
↧