Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
saviornt's avatar
saviornt
Protege
12 years ago

Getting the OVR transform to mechanim?

I understand that for mechanim, I'll need to use the Animator.SetLookAtPosition in order to move a character's head/neck; but how do I go about linking the vector3 that it needs to the OVRCamera's camera.transform.rotation?

So far, my code is as follows:

using UnityEngine;
using System;
using System.Collections;

[RequireComponent(typeof(Animator))]

public class playerIK : MonoBehaviour {
Animator animator;
[HideInInspector]
public Transform target;

void onAnimatorIK (){
Vector3 target = OVRCamera.NeckPosition; <---- this does not work, at all :(
animator.SetLookAtPosition (target);
animator.SetLookAtWeight(1.0f);
}

void Start ()
{
Animator = GetComponent<Animator>();
}
}

8 Replies

Replies have been turned off for this discussion
  • Sort of got it working, kind of. By re-arranging the heirarchy so that the ForwardDirection game object is between the neck and the head, it gives horizontal rotation, however, it does not give vertical rotation (character will look left and right, but not up and down).

    Soo.. I'm 1/2 there :S
  • Day 14(ish)

    Came across a script that "should" animate the character rig. It doesn't though.

    Attached an empty game object to the OVR Camera Controller, made it eye level with the character, and moved it away a bit. This is successful, it rotates around the body according to where the user is looking. This gives the position where mechanim should point the head.

    The next step was to create a script, and attach that script.. somewhere. So far I've tried on the main model, and further into the heirarchy of the model to no avail.

    If anyone is interested, here is the script:


    using UnityEngine;
    using System.Collections;
    [RequireComponent(typeof(Animator))]
    public class headController : MonoBehaviour {

    public Transform lookTransform; // Empty game object, level with the OVRCameraController, offset along the Z axis

    void OnAnimatorIK(){
    if (lookTransform != null) {
    Animator animator = GetComponent<Animator>();
    animator.SetLookAtPosition(lookTransform.position);
    animator.SetLookAtWeight(1.0f);
    }
    }


    I've also tried the script using the Update() and LateUpdate() methods or whatever, and have also added:

    void Start (){
    animator = GetComponent<Animator>();
    }


    with all 3.. still nada. Irritating :\

    Something else I just ran in to today, when using the Character Motor, whenever I press a button, my framerate drops from 70-75fps to 3 fps... wth?
  • Thanks for the linky, not sure how google missed it :(

    That was my fallback idea, to use a basic animation script to control the headbone, however, I would like to use mechanim's IK due to the fact that if the player looks too far in a direction (such as behind them), their body will apply a kinematic solution to the joints / bone structure.

    I have also heard that by using non-mechanim based character movements, that it can do weird things to mechanim character animations (of course, I haven't tested that out myself, since you just provided an answer to my own going migraine :) ).. is that true?
  • owenwp's avatar
    owenwp
    Expert Protege
    You will get better results with something like FinalIK. That will move the spine as well so that your head can follow the Rift camera.
  • jvr's avatar
    jvr
    Honored Guest
    Some IK troubleshooting tips:

    1. I assume you have Unity Pro

    2. Check if the official Unity example works for you:
    https://www.assetstore.unity3d.com/#/content/5328
    Inverse Kinematics Example scene

    When in play mode make sure to tick the "Activate IK" checkbox to make the character look at the ball.

    3. If that works then import your character into that example and apply the "IK" script from that example to it and the same animation controller. If it's not working then your character most likely needs to be properly setup for Mecanim.

    4. If it's not working when you use your animation controller then make sure that you have IK Pass enabled on the Base Layer of it.
  • "saviornt" wrote:
    I would like to use mechanim's IK due to the fact that if the player looks too far in a direction (such as behind them), their body will apply a kinematic solution to the joints / bone structure.


    Good point. I haven't tried it myself, but you should be able to use IK with something like SetLookAtWeight( weight: 1, bodyWeight: 0.2f, headWeight: 0.9f) in OnAnimatorIK() to make the upper body move a bit as well and then overlay the actual head rotation in LateUpdate() (which is executed after OnAnimatorIK), so the head looks the exact same direction as the Rift.
  • Anonymous's avatar
    Anonymous
    I know this is an older thread, but the only problem with your OnAnimatorIK function was that it expects an integer to be passed in. If you are still trying to use OnAnimatorIK, or anyone else stumbles across this thread try this.

    void OnAnimatorIK(int index){
    if (lookTransform != null) {
    Animator animator = GetComponent<Animator>();
    animator.SetLookAtPosition(lookTransform.position);
    animator.SetLookAtWeight(1.0f);
    }