Created
July 29, 2018 06:22
-
-
Save n-taku/20b7b4e3a2fcfb6ac9f56fddebeaf535 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 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