Skip to content

Instantly share code, notes, and snippets.

@daichi-takezawa
Last active April 30, 2021 05:13
Show Gist options
  • Save daichi-takezawa/eceb3eca90d024ce308dfcbc8883a7ba to your computer and use it in GitHub Desktop.
Save daichi-takezawa/eceb3eca90d024ce308dfcbc8883a7ba to your computer and use it in GitHub Desktop.
Vector3 latestPos;
public void FixedUpdate()
{
//前フレームとの位置の差から進行方向を割り出してその方向に回転します。
Vector3 differenceDis = new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(latestPos.x, 0, latestPos.z);
latestPos = transform.position;
if (Mathf.Abs(differenceDis.x) > 0.001f || Mathf.Abs(differenceDis.z) > 0.001f)
{
if (movingDirecion == new Vector3(0, 0, 0)) return;
Quaternion rot = Quaternion.LookRotation(differenceDis);
rot = Quaternion.Slerp(rb.transform.rotation, rot, 0.2f);
this.transform.rotation = rot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment