Hi, the following are my scripts for a homing missile, it works, however it just flies straight ahead, without any tracking at all, any help would be appreciated (Yes the tags and rigidbodies have been applied) This tutorial was used: http://www.youtube.com/watch?v=s87zyCwHLgE This one is attached to the main camera to fire the missile Code (csharp): var projectile : GameObject; function Update () { if (Input.GetButtonDown ("Fire1")) { Instantiate (projectile, transform.position, transform.rotation); } } This one is attached to the missile mesh Code (csharp): var Speed : float; var Turn : float; function Update (){ var targets: GameObject[] = GameObject.FindGameObjectsWithTag("Enemy"); var closest: GameObject; var ClosestDist = Mathf.Infinity; for (Target in targets){ var dist = (transform.position - Target.transform.position).sqrMagnitude; if(dist < ClosestDist){ ClosestDist = dist; closest = Target; } } Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(closest.transform.position-transform.position),Turn*Time.deltaTime); transform.position+=transform.forward*Speed*Time.deltaTime; } function OnCollisionEnter(collision : Collision){ Destroy (gameObject); }
i think this is the problem Code (csharp): Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(closest.transform.position-transform.position),Turn*Time.deltaTime); TO: Code (csharp): transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(closest.transform.position-transform.position),Turn*Time.deltaTime);
There are numerous problems with this script. 1. You search for a new target every frame. This is not only bad (REALLY bad), but it is most likely confusing your missile. 2. You are manipulating position/rotation on a rigidbody. This is also bad. The best solution Ive found for my missiles is setting the velocity directly, and setting the rotation via MoveRotation. In your situation, I will add function for setting a target. This is not only better, but its the correct way to do what youre trying to do. I use c#, so I cant be sure this will be grammatically correct. Code (csharp): var _velocity : float = 10; var _torque: float = 5; var _target : Transform; var _rigidbody : Rigidbody; function Start() { _rigidbody = transform.rigidbody; } function Fire() { var distance = Mathf.Infinity; for (var go : GameObject in GameObject.FindGameObjectsWithTag("Enemy")) { var diff = (go.transform.position - position).sqrMagnitude; if(diff < distance) { distance = diff; _target = go.transform; } } } function FixedUpdate() { if(_target == null || _rigidbody == null) return; //missiles forward momentum _rigidbody.velocity = Vector3(0,0,_velocity); //missile turning momentum var step = _torque * Time.deltaTime; var relativePos = _target.position - transform.position; var targetRotation = Quaternion.LookRotation(relativePos); var rotation = Quaternion.RotateTowards(transform.rotation, targetRotation , step); _rigidbody.MoveRotation(_rigidbody.rotation * rotation); } function OnCollisionEnter(collision : Collision) { Destroy(gameObject); } I think ive got my rotation/velocity stuff right. If it doesnt work, let me know and ill check my actual code (this was done from memory)
tutorials. plural. multiple. Go do some more. You should be able to fix the syntax. Also, this tutorial is obviously terrible if what you posted is the example code.
anyway, this is code that doesn't contain errors... incidentally, neither did the original script, however it wasn't working correctly... Here is a complete js script (note: there should be NO other code in this file) This works correctly. If it doesnt, youve done something else wrong. Code (csharp): #pragma strict var _velocity : float = 10; var _torque: float = 5; var _target : Transform; var _rigidbody : Rigidbody; function Start() { _rigidbody = transform.rigidbody; Fire(); } function Fire() { var distance = Mathf.Infinity; for (var go : GameObject in GameObject.FindGameObjectsWithTag("Enemy")) { var diff = (go.transform.position - transform.position).sqrMagnitude; if(diff < distance) { distance = diff; _target = go.transform; } } } function FixedUpdate() { if(_target == null || _rigidbody == null) return; _rigidbody.velocity = transform.forward * _velocity; var targetRotation = Quaternion.LookRotation(_target.position - transform.position); _rigidbody.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, _torque)); } function OnCollisionEnter(collision : Collision) { Destroy(gameObject); }
Thanx JamesLeeNZ for this script. ill give it a go. also, is it too accurate or can some error in tracking be factored in? anyway, ill see if i can do it myself
If you mess around with _torque you can adjust how fast it can track its target. The lower the torque, the slower it will turn towards its target. alternatively you could adjust the targeted center by adding some noise in (_target.position + new Vector3(val,val,val))
thank you sir, cant believe it was that easy. i think this script is the best ive found for a homing missile
Heh, it took me several iterations to come up with this solution. Tried a few different methods, but this one gave me the best missile behaviour.
Seems to work.. odd the missile kinds of steers away before hitting the cube. EDIT: my bad one my layers was affecting the missile. (the rain) Got this revised code working in under 1 minute. Lol spent over 1 hour on your original without getting it to work function OnTriggerEnter (theCollider : Collider) { Debug.Log("HIT TAG: " + theCollider.tag + "HIT NAME: " + theCollider.gameObject.name ); } //in update if(target == null) { Destroy(this.gameObject); return; //now the object is destroyed the script is no longer valid so we should simply return }
I need convert FixedUpdade to c# can you help me JamesLeeNz? Code (CSharp): using UnityEngine; using System.Collections; public class Missil : MonoBehaviour { public float velocidade = 100f; public float torque = 25f; public Rigidbody missilRb; public Transform alvo; void Start () { missilRb = transform.GetComponent<Rigidbody> (); Fire (); } void FixedUpdade() { if (alvo == null || missilRb == null) return; missilRb.velocity = transform.forward * velocidade; var targetRotation = Quaternion.LookRotation(alvo.position - transform.position); missilRb.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, torque)); Debug.Log(missilRb.velocity); } void Fire() { float distancia = Mathf.Infinity; GameObject mairPerto; Vector3 position = transform.position; foreach(GameObject go in GameObject.FindGameObjectsWithTag("Enemy")) { Vector3 diff = go.transform.position - position; float distanciaAtual = diff.sqrMagnitude; if(distanciaAtual < distancia) { distancia = distanciaAtual; alvo = go.transform; } } } }