Homing bullets - rotating rigidbodies with existing velocity

Hi guys,

what follows is a script that is on an enemy’s bullet. I’m trying with the latter part of the script to make the bullet home in by changing the rotation of it, but the problem I’ve got is that I cant figure out how to change the angle of it - given that it already has an assigned velocity from a script which fires the bullet (its a rigidBody).

Here is the attempt at getting it to rotate -

var BulletTimeout = 6.0;
var homingSpeed = 0.1;

function Start() {
Invoke(“Kill”, BulletTimeout);
}

function Kill(){
Destroy(gameObject);
}

function Update () {

var targetPoint = GameObject.FindWithTag(“Player”).transform.position;

// forward direction in enemy’s coordinate space
var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = targetPoint - transform.position;

if((Vector3.Distance(transform.position, targetPoint) < 80)) {

//5 is the field of view angle checking for player
var theAngleBetween = Vector3.AngleBetween(forward, targetDirection) * Mathf.Rad2Deg;

if(theAngleBetween < 40) {

var rot = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
// homingSpeed is the speed it turns
rot = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * homingSpeed);

rigidbody.velocity = rot * rigidbody.velocity;
}
}

}

And here is the script that fires the bullet -

var fieldOfViewShoot = 10.0;
var enemyBullet : Rigidbody;
var shootFreq = 0.3;
var timer : float;
var enemyShootSpeed = 10.0;

function Start () {

timer = 0.0;

}

function Update () {

timer += Time.deltaTime;

var targetPoint = GameObject.FindWithTag(“Player”).transform.position;

// forward direction in enemy’s coordinate space
var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = targetPoint - transform.position;

//10 is the field of view angle checking for player
if(Vector3.AngleBetween(forward, targetDirection) * Mathf.Rad2Deg < fieldOfViewShoot) {

if((Vector3.Distance(transform.position, targetPoint) < 80) (timer > shootFreq)) {

Debug.Log(“nearEnough!”);

timer = 0.0;

var thingIshot : Rigidbody = Instantiate (enemyBullet, transform.position, transform.rotation);

thingIshot.velocity = transform.TransformDirection(Vector3(0,0, enemyShootSpeed));

Physics.IgnoreCollision(thingIshot.collider, transform.root.collider);
Physics.IgnoreCollision(thingIshot.collider, GameObject.FindWithTag(“Destructible”).transform.root.collider);

}
}

}

any help with how to do this is much appreciated! I just want a gentle “homing bullet” effect. So rather than keeping the bullet going at its current velocity I want to angle it toward the player a little as it spots the player in its field of view.

Thanks folks

Will

Will, when you post code could you use the code brackets? It makes it much easier to read code. Either click the “Code” button near the top of the posting window when starting your code and then again when you are done with your code portion, or use and to bracket the code portions.

For the gentle turn, have you looked into adding a small force to cause the bullet to turn?

Sorrry I hadnt noticed the code button, i will use it in future thanks for the tip, I’m currently tinkering with forces, yep, cheers :slight_smile:

Quaternion.Slerp may help.

It looks like you are checking if the angle between the bullet’s trajectory and the diretion to the target is < 40 degrees. Isn’t this going to generally be the case until the bullet is very close, as long as I fire in the general direction? Isn’t that allowing for 40 degrees either side of straight forward of the bullet? Also, what is the general distance of the target when the bullet is first created? I assume it is much greater than 80, otherwise there will not be to many opportunities to adjust the trajectory.

On thing you may also want to consider. Instead of doing this every Update

var targetPoint =   GameObject.FindWithTag("Player").transform.position;

you might consider doing this once in something like Start() or set targetPoint from the script that creates the bullet.

One of the unity Posse, Joe I think posted a homing missile script here somewhere so do a forum search for it.
AC