Home Unity

Unity - Bone rotation to track mouse?

polycounter lvl 6
Offline / Send Message
Malloy polycounter lvl 6
Hi there,

I basically have a character controller with a custom motor script. I'm new to Unity and am finding it very splintered compared to other engines but am required to use it for a university assignment. For my assignment I intend to create a 2.5D side-scroller as it will be achievable in the time span given by the uni. Everything was going swimmingly until I had to 'AddAnimationMix' some bones (stop them from animating so that they can become manipulated) and set it to track the mouse. I have googled and searched the crap out of the unity help forums but I am still to no fix. Does anybody know an assured 'script' to bone [gameObject] method so I can carry on with developing the character controls?

p.s I'm using Javascript script

Thanks!

Replies

  • jerry
    Options
    Offline / Send Message
    I think you can change the bones' localtransform in the LateUpdate function. Since this gets called after the animations have been processed you can override the rotation.

    Hope this works.
  • Malloy
    Options
    Offline / Send Message
    Malloy polycounter lvl 6
    Thanks for the response! I have however been running it thought the LateUpdate function. I tried to apply the logic of how I've done it in 2D side-scrollers but for some reason local transform.rotate-ing the Z axis rotates it along the Y simultaneously... I've had a good sleep since yesterday and i've practically just woken up. Hopefully with a fresh mind i'll get to the bottom of this! (if I do, i'll post findings here incase it helps others) :)
  • LoTekK
    Options
    Offline / Send Message
    LoTekK polycounter lvl 17
    Assuming you're actually using
    <bone>.localRotation.z
    
    that's likely where your multiple axis rotation is coming from.

    localRotation takes Quaternions, and not euler (xyz) angles, so you'll want to use
    <bone>.localEulerAngles.z
    
  • jerry
    Options
    Offline / Send Message
    That should do the trick, I forgot about that actually :O
  • Malloy
    Options
    Offline / Send Message
    Malloy polycounter lvl 6
    I did implement that and I now have the issue of screen/mouse calibration. When the mouse leaves the screen, it resets the value. Using the axis ("mouse Y")... Should I use the origin of Input.mousePosition.y against the screenHeight?

    edit: Wow I'm retarded... can easily set the mouse co-ordinate back to screen boundary if it crosses.
  • Malloy
    Options
    Offline / Send Message
    Malloy polycounter lvl 6
    Yeah just a bump to say I solved it:
    var minAngle = -45; 
    var maxAngle = 45;
    
    private var tempVector : Vector3;
    private var tempVector2 : Vector3;
    
    private var inputRotation : Vector3;
    private var rotateCheck;
    
    function LateUpdate() {
    
    tempVector2 = new Vector3(Screen.width * 0.5f,Screen.height * 0.5f,0); 
    
    tempVector = Input.mousePosition;
    
    inputRotation = tempVector - tempVector2;
    
    var boneScreenPos = Camera.main.WorldToScreenPoint(transform.position);
    
    if (Input.mousePosition.x < boneScreenPos.x)
    {
      transform.rotation = Quaternion.LookRotation(inputRotation, Vector3.forward);
      transform.localEulerAngles.x = 0;
      transform.localEulerAngles.z = 0;
    }
    else if (Input.mousePosition.x > boneScreenPos.x)
    {
      transform.rotation = Quaternion.LookRotation(inputRotation, -Vector3.forward);
      transform.localEulerAngles.x = 0;
      transform.localEulerAngles.z = 0;
    }
    
    var angle = transform.localEulerAngles.y; 
    
    if (angle > 180) 
        {
        angle -= 360; 
        }
    if (angle <= -180) 
        {
        angle += 360; 
        }
    
    transform.localEulerAngles.y = Mathf.Clamp(angle, minAngle, maxAngle);
    }
    

    Just attach this to the bone in your skinned mesh and it'l track the angle and rotate to face the mouse. (This also feeds into a Globals script that conditions the player's facing direction depending on whether the mouseX is greater than the players position on the screen. This effects the above smoothness of rotation. Side Scroller only but could be adapted to other types of play.)
Sign In or Register to comment.