Hi everyone,
I’d like to attempt… if not succeed at modifying Joachims homing missile script (see below) to work on my enemy robots, currently seem here -
www.willgoldstone.com/robotest
At the moment they use this script to shoot at the player when you walk in front of them -
Attached to the GUN -
var fieldOfViewShoot = 10.0;
var enemyBullet : Rigidbody;
var shootFreq = 0.3;
var enemyShootSpeed = 10.0;
var enemyProx = 50;
var timer : float;
function Start () {
timer = 0.0;
}
function Update () {
timer += Time.deltaTime;
var targetPoint = GameObject.FindWithTag("Player").transform.position;
var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = targetPoint - transform.position;
if(Vector3.AngleBetween(forward, targetDirection) * Mathf.Rad2Deg < fieldOfViewShoot) {
if((Vector3.Distance(transform.position, targetPoint) < enemyProx) (timer > shootFreq)) {
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);
}
}
}
Attached to the BULLET’S prefab -
var BulletTimeout = 6.0;
var homingSpeed = 0.5;
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) < 500)) {
//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.angularVelocity = rot * rigidbody.angularVelocity;
}
}
}
And i’ve just started looking at Joachims homing missile script and would like to use elements of it in order to make the enemy bots bullets lean toward the player - at the moment if you play the game you’ll notice theyre easy to avoid.
The first prob with the script hes posted is that its for a player, and therefore is only triggered on fire, as opposed to mine which is triggered a lot - therefore enemies would be firing rays at all sorts of things, rather than looking for the player.
So, my main question I suppose is, what is the theory behind using the part of Joachims script that homes in on a ray selected object, and how can I use this to make the bullets my enemies fire head toward the player. I attempted to make this happen in my own script (see the one attached to BULLET above) but this doesnt really work.
I know this is a big question… well to me anyway! Any help much appreciated as always, see Joachim’s Homing missile for FPS controller below -
/*
The homing missile every frame simply rotates towards the target
The actual physics is done by setting up the drag in the rigidbody (5 works well)
and a relative force along the zaxis in the constanct force (
relative.z = 50 works well)
*/
var target : Transform;
var rotationSpeed = 50.0;
private var relativeTargetPoint = Vector3.zero;
private var absoluteTargetPoint = Vector3.zero;
function Start ()
{
rigidbody.freezeRotation = true;
}
function Update () {
// Update the target point
if (target)
absoluteTargetPoint = target.TransformPoint(relativeTargetPoint);
// Rotate towards the target
var oldDirection = transform.TransformDirection(Vector3.forward);
var direction = absoluteTargetPoint - transform.position;
direction = Vector3.RotateTowards(oldDirection, direction, rotationSpeed * Mathf.Deg2Rad
* Time.deltaTime, 1);
transform.rotation = Quaternion.LookRotation(direction);
}
function SetTarget (t : Transform) {
target = t;
}
function SetTargetAbsolutePosition (position : Vector3) {
absoluteTargetPoint = position;
if (target)
relativeTargetPoint = target.InverseTransformPoint(absoluteTargetPoint);
}
@script RequireComponent(Rigidbody, ConstantForce, Rocket)
Cheers
Will
[/code]