Search Unity

Homing bullets - rotating rigidbodies with existing velocity

Discussion in 'Scripting' started by willgoldstone, Nov 8, 2006.

  1. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    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
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    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
    Code (csharp):
    1.  and
    to bracket the code portions.

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

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    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 :)
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Quaternion.Slerp may help.
     
  5. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    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
    Code (csharp):
    1.  
    2. var targetPoint =   GameObject.FindWithTag("Player").transform.position;
    3.  
    you might consider doing this once in something like Start() or set targetPoint from the script that creates the bullet.
     
  6. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    One of the unity Posse, Joe I think posted a homing missile script here somewhere so do a forum search for it.
    AC