Last active
June 28, 2020 14:10
-
-
Save gamebox777/d43a0d5782bda1731aae0780319843a9 to your computer and use it in GitHub Desktop.
ミサイルの向きスクリプト
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Missile01Script : MonoBehaviour | |
{ | |
/// <summary> ターゲット(キューブ)</summary> | |
public GameObject mTarget; | |
/// <summary> 向きを変えるスピード</summary> | |
public float mTurnSpeed = 50f; | |
/// <summary> ターゲットへの移動スピード </summary> | |
public float mMoveSpeed = 5; | |
// Update is called once per frame | |
void Update() | |
{ | |
#if false | |
//瞬時にターゲットの方に向かせる | |
ChangeDirectionNoTime(); | |
#else | |
//補間しながらターゲットの方を向く | |
ChangeDirectionInterpolation(); | |
//ターゲットの方に移動 | |
MoveToTarget(); | |
#endif | |
} | |
/// <summary> | |
/// 瞬時にターゲットの方を向く | |
/// </summary> | |
void ChangeDirectionNoTime() | |
{ | |
transform.LookAt(mTarget.transform); | |
} | |
/// <summary> | |
/// 補間しながらターゲットの方を向く | |
/// </summary> | |
void ChangeDirectionInterpolation() | |
{ | |
Vector3 direction = mTarget.transform.position - transform.position; | |
direction.Normalize(); | |
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(direction), mTurnSpeed * Time.deltaTime); | |
} | |
/// <summary> | |
/// ターゲットの方向へ移動させる | |
/// </summary> | |
void MoveToTarget() | |
{ | |
Vector3 tVec = transform.rotation.eulerAngles.normalized; | |
transform.Translate(Vector3.forward * mMoveSpeed * Time.deltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment