Question by
agusval1994 · Jun 25, 2020 at 12:31 AM ·
rotationplayerlookatsphere
How to rotate object in sphere
Hi, I have my character on a planet (sphere) and when it moves i want to look in the direction its moving. I have this code but the rotation doesn't work correctly.
What should I do?
Thanks.
public float walkSpeed = 6;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Rigidbody rigidbody;
void Awake()
{
rigidbody = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = CrossPlatformInputManager.GetAxis("Horizontal");
float moveV = CrossPlatformInputManager.GetAxis("Vertical");
Vector3 moveDir = new Vector3(-moveH, 0, -moveV).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, .15f);
rigidbody.velocity = new Vector3(moveH * walkSpeed, rigidbody.velocity.y, moveV * walkSpeed);
if (moveH != 0f || moveV != 0f)
{
Quaternion rot = Quaternion.LookRotation(rigidbody.velocity);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, 5 * Time.deltaTime);
}
}
void FixedUpdate()
{
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + localMove);
}
Comment