Skip to content

Instantly share code, notes, and snippets.

@n-taku
Created July 29, 2018 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n-taku/a973f7477f995f4c631e32343b8d7fe4 to your computer and use it in GitHub Desktop.
Save n-taku/a973f7477f995f4c631e32343b8d7fe4 to your computer and use it in GitHub Desktop.
追尾スクリプト
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