Last active
February 1, 2019 09:31
-
-
Save HassakuTb/243ebf2163cf2e68fc8c99843f2e132a to your computer and use it in GitHub Desktop.
Homig object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace Citrus.VectorComputes | |
{ | |
/// <summary> | |
/// 対象を向くように回転しながら前進するオブジェクト | |
/// </summary> | |
public class Homing : MonoBehaviour | |
{ | |
/// <summary> | |
/// 追尾対象 | |
/// </summary> | |
public Transform Target; | |
/// <summary> | |
/// 追尾速度 | |
/// </summary> | |
public float Speed; | |
/// <summary> | |
/// 旋回速度 | |
/// </summary> | |
public float AngularSpeed; | |
private void Update() | |
{ | |
if (Target == null) UpdateRotation(); // 対象がいないなら直進する | |
UpdatePosition(); | |
} | |
/// <summary> | |
/// 回転方向を更新する | |
/// </summary> | |
private void UpdateRotation() | |
{ | |
Vector3 positionDiff = Target.position - transform.position; | |
float angleDelta = AngularSpeed * Time.deltaTime; | |
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(positionDiff), angleDelta); | |
} | |
/// <summary> | |
/// 位置を更新する | |
/// </summary> | |
private void UpdatePosition() | |
{ | |
float speedDelta = Speed * Time.deltaTime; | |
transform.position += transform.TransformDirection(transform.forward) * speedDelta; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment