Search Unity

modifying the first person controller to be a very simple AI

Discussion in 'Scripting' started by spiralgear, Dec 13, 2007.

  1. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    its been a long time since ive done any programing and im having trouble with this small task



    i just want to change the fpc script to walk towards a target object instead of using player input, but i cant figure out how to tell it which way it should be facing, i tried using look at but that just snaps the rotation to the targets direction instantly



    what formula do i use to make var A contain the rotation needed to face the target object


    i hope i wrote that in a way that makes sense
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    You subtract the target's position vector from "your" position vector. That creates a direction vector pointing from this pos to the target. The magnitude of that direction vector is the distance to target.

    Then you would use the Quaternion.LookRotation function to create a target rotation, then lerp from the current rotation to the target rotation smoothly.

    C#
    Code (csharp):
    1.  
    2. //how quickly we turn towards the target
    3. float rotationSpeed = 0.5F;
    4.  
    5. Vector3 dir = target.transform.position - transform.position;
    6.  
    7. Quaternion targetRotation = Quaternion.LookRotation(dir.normalized);
    8. float str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
    9. transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
    10.  
    I don't know if you'd have to do anything else to get the FPC to move, but this is how you figure out how to move.

    -Jeremy
     
  3. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    thanks now i understand how it works, but im having trouble writing it in javascript so i can put it into the walker code
     
  4. spawrks

    spawrks

    Joined:
    Nov 16, 2007
    Posts:
    89
  5. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528



    ahhh thanks, the sentry gun script in the fps tutorialhad exactly what i was looking for
     
  6. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    one more thing



    Code (csharp):
    1. var target : Transform;
    2. var turnspeed=5.0;
    3.  
    4.  
    5.  
    6. function Update () {
    7.    
    8.  
    9.  
    10. var targetRotation = Quaternion.LookRotation (target.position- transform.position,Vector3.up);
    11. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,Time.deltaTime * turnspeed);
    12.  
    13. }

    this my code now and it works fine, but how can i get it to face only the x axis position of the target
     
  7. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    The Quaternion.LookRotation takes the selected up vector as the second parameter, which is currently positive Y axis (0, 1, 0, or Vector3.up). You should be able to just change what direction it uses for up. I haven't tried it myself though, never needed to.

    http://unity3d.com/support/documentation/ScriptReference/Quaternion.LookRotation.html

    One thing that isn't clear, do you want your object's x rotation to match the target's x rotation, or do you want you object to face in the direction to your other object, but pointing a different axis towards it? If it's the latter, try what I said above, otherwise just modify the object's x rotation to match the target's.

    HTH,
    -Jeremy
     
  8. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528


    i want it to face the target object with the z axis, and ignore the targets height, so the object stays level even if the target is 100 feet up



    i have a solution that works for now though