Created
July 29, 2018 06:17
-
-
Save n-taku/a973f7477f995f4c631e32343b8d7fe4 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
public class Missile : MonoBehaviour | |
{ | |
public Transform target; | |
public Vector3 velocity; | |
public Vector3 position; | |
public float period; | |
//最大加速度 | |
public float maxAcceleration = 100; | |
//ランダムに加える力 | |
public float randomPower; | |
public float randomPeriod; | |
void Update() | |
{ | |
if (target == null) | |
return; | |
var acceleration = Vector3.zero; | |
var diff = target.position - position; | |
//速度velocityの物体がperiod秒後にdiff進むための加速度 | |
acceleration += (diff - velocity * period) * 2f / (period * period); | |
if (0 < randomPeriod) | |
{ | |
var xr = Random.Range(-randomPower, randomPower); | |
var yr = Random.Range(-randomPower, randomPower); | |
var zr = Random.Range(-randomPower, randomPower); | |
acceleration += new Vector3(xr, yr, zr); | |
} | |
if (acceleration.magnitude > maxAcceleration) | |
{ | |
acceleration = acceleration.normalized * maxAcceleration; | |
} | |
period -= Time.deltaTime; | |
randomPeriod -= Time.deltaTime; | |
if (period < 0f) | |
return; | |
velocity += acceleration * Time.deltaTime; | |
position += velocity * Time.deltaTime; | |
transform.position = position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment