Skip to content

Instantly share code, notes, and snippets.

@n-taku
Created July 29, 2018 06:22
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/20b7b4e3a2fcfb6ac9f56fddebeaf535 to your computer and use it in GitHub Desktop.
Save n-taku/20b7b4e3a2fcfb6ac9f56fddebeaf535 to your computer and use it in GitHub Desktop.
誘導スクリプト
using UnityEngine;
public class Missile : MonoBehaviour
{
public Transform target;
public float torqueRatio;
public float speed;
void FixedUpdate()
{
if (target == null)
return;
//向きをターゲットの方に向ける
var diff = target.transform.position - transform.position;
var target_rot = Quaternion.LookRotation(diff);
var rot = target_rot * Quaternion.Inverse(transform.rotation);
if (rot.w < 0f)
{
rot.x = -rot.x;
rot.y = -rot.y;
rot.z = -rot.z;
rot.w = -rot.w;
}
var torque = new Vector3(rot.x, rot.y, rot.z) * torqueRatio;
GetComponent<Rigidbody>().AddTorque(torque);
//まっすぐ進む
GetComponent<Rigidbody>().velocity = transform.forward * speed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment