Rotate Rigidbody 2D towards velocity - Unity Answers
ここにあるがUnityScriptな上、変数やパフォーマンス的に書き直し。
|cs| using UnityEngine; using System.Collections;
[RequireComponent(typeof(Rigidbody2D))] public class CannonProjectile : MonoBehaviour { public float rotateSpeed = 18.0f; private Transform myTransform; private Rigidbody2D myRigidbody2D;
// Use this for initialization
void Start ()
{
myTransform = transform;
myRigidbody2D = this.rigidbody2D;
}
// Update is called once per frame
void Update ()
{
Vector2 dir = myRigidbody2D.velocity;
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion quaternion = Quaternion.AngleAxis (angle, Vector3.forward);
myTransform.rotation = Quaternion.RotateTowards (myTransform.rotation, quaternion, rotateSpeed * Time.deltaTime);
}
} ||<
これでOK。
Unityではじめる2Dゲーム作り徹底ガイド スマートフォンでも遊べる本格ゲーム開発に挑戦
- 作者: 大野功二
- 出版社/メーカー: SBクリエイティブ
- 発売日: 2014/10/29
- メディア: 大型本
- この商品を含むブログを見る