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