Search Unity

scripted character pointing

Discussion in 'Scripting' started by EricKramp, Dec 11, 2007.

  1. EricKramp

    EricKramp

    Joined:
    Apr 18, 2007
    Posts:
    54
    I'm trying to get my character to point at an object in the scene that you click on. The big hurdle that I've been bashing my head on is the fact that the X axis is going down the length of the shoulder bone rather than the Z. The other problem is that the pointing needs to go on top of the idle animation.

    It seems like the easiest way to make it work, would to calculate a LookAt rotation using the X-axis as forward rather than the Z. Then I could snap a null object into the appropriate rotation and animate the rotation of the shoulder bone to match.

    I discovered from a previous thread that putting the rotation information for the shoulder in a LateUpdate will override the animation. So now all I need is a way to calculate the appropriate LookAt. Unfortunately that one is way beyond my meager skills.

    Does this seem like a fairly sound way to approach the problem? and if so, anyone know how to calculate the appropriate LookAt?

    Thanks!

    Eric
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    transform.LookAt(pos) is a shortcut for another function:
    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.LookRotation(pos-transform.position, Vector3.up);
    3.  
    That is, the above code function precisely as .LookAt does. The second parameter there is what direction should be "up" - you can probably see where I'm going with this.If you use transform.parent.right or -transform.parent.right or some permutation of something like that it should work. If not at least you know where to stick your desired "up" and hopefully can figure it out from there.