- Home /
Guided missile help c#
Basically im trying to make a physics based guided missile script, so far i have this:
public Transform target;
public float force = 0.5f;
private bool ifTarget = false;
public void SetTarget(Transform helTarg){
target = helTarg;
ifTarget = true;
}
void Update () {
if(ifTarget){
rigidbody.AddForce(transform.up * 100);
Vector3 targetDelta = target.position - transform.position;
//get the angle between transform.forward and target delta
float angleDiff = Vector3.Angle(transform.forward, targetDelta);
// get its cross product, which is the axis of rotation to
// get from one vector to the other
Vector3 cross = Vector3.Cross(transform.forward, targetDelta);
// apply torque along that axis according to the magnitude of the angle.
rigidbody.AddTorque(cross * angleDiff * force);
}
}
But this only causes weird behaviour with the missile, as seen here:
http://tristanjc.com/unity/newweb.html (Target a tank with Tab and Press F for HellFire missiles, then click)
How would you go about a proper or a simple way to do this type of thing, also how would you make it go towards a target without using addForce, as that increases the speed at the same time.
I suggest not using rigidbody physics at all when making projectiles
Here's a very simple script, with constant velocity that follows the target around:
public Transform target;
public float speed = 3f;
private bool ifTarget = false;
public void SetTarget(Transform helTarg){
target = helTarg;
ifTarget = true;
}
void Update () {
if(ifTarget){
Vector3 toTarget = target.position - transform.position;
rigidbody.velocity = toTarget.normalized * speed;
transform.forward = toTarget.normalized;
}
}
I simply set the velocity to be towards the target, and its magniuted to be speed
(which is 3). I also rotate the missile every frame to make sure it's pointing at the target.
Note, for this to work, the missile's forward
must be the missile's head. If the missile's head is pointing upwards ins$$anonymous$$d of forward, use the following line ins$$anonymous$$d of transform.forward =
:
transform.up = toTarget.normalized;
oh sorry, forgot about this question. I came up with this in the end:
public class Hellfire : $$anonymous$$onoBehaviour {
private Transform target;
private float force = 2f;
private bool ifTarget = false;
private float starttime;
public void SetTarget(Transform helTarg){
target = helTarg;
ifTarget = true;
starttime = Time.time + 0.5f;
transform.parent = null;
gameObject.AddComponent<Rigidbody>();
}
void FixedUpdate () {
if(ifTarget){
if(target == null){
rigidbody.velocity = (transform.forward * 1500 * Time.deltaTime);
} else {
if(Time.time > starttime){
Vector3 dir = target.position - transform.position;
Quaternion rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, force * Time.deltaTime);
}
rigidbody.velocity = (transform.forward * 1500 * Time.deltaTime);
}
}
}
}
Answer by robertbu · Oct 21, 2013 at 05:10 AM
What to use will partly depend on your functionality. For example, I'd pick a different solution depending on whether you want to allow the missile to miss the target or not. For a non-miss you can use:
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
transform.LookAt(target.position);
If you want to use a Rigidbody, I'd try this first:
Make sure your missile is constructed so that the nose of the missile is pointed to forward. That is, when the rotation is (0,0,0), the nose should be looking at positive 'Z'.
In the inspector increase the Drag significantly.
Then try something like the following (executed in FixedUpdate()):
rigidbody.AddForce((target.position - transform.position)*amount);
transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
Depending on the setting of 'Drag', this will act fairly realistically if the target is moved. Here is an alternate couple of lines that makes the game object perfectly track the target:
rigidbody.velocity = ((target.position - transform.position) * speed);
transform.LookAt(target.position);
If you add (in the second solution) some drag to the missile, you will get a lot better accuracy. You will also have to add more speed to the missile.