0
\$\begingroup\$

I'm trying to make my character bone "chest" rotate smoothly when face enemy and get back to his stand. Can anyone help me with this ?

public Transform Target;
public Vector3 Offset;

Animator anim;
Transform chest;

public float rotation_speed;
public bool aim_at_traget = false;

void Start()
{
    anim = GetComponent<Animator>();
    chest = anim.GetBoneTransform(HumanBodyBones.Chest);
}

void LateUpdate()
{
    if (aim_at_traget == true) {
    // smooth rotation to face target
    chest.LookAt(Target.transform.position);
    chest.rotation = chest.rotation * Quaternion.Euler(Offset);
    }
    else {
        // get back to you stand smoothly 
    }
}
\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

In LateUpdate(), I would change the code to be this:

if(aim_at_target == true) {
   Quaternion rot = Quaternion.LookRotation(Target.transform.position - transform.position);
   chest.rotation = Quaternion.Slerp(chest.rotation, rot, Time.deltaTime * 
   YOUR_SMOOTHNESS_VALUE);
}

Not sure what 'Quaternion.Euler(Offset)' is exactly but you probably can just multiply it in the second parameter of Quaternion.Slerp().

I did not test this, so may have a small mistake, I'm just giving you an idea to proceed and make it a smooth rotation.

\$\endgroup\$
10
  • \$\begingroup\$ Thank you for your respond. you code give me error on first line. I tried to do what you said. Using Slerp but still I can't see smooth movement. What else you advice me to do ? \$\endgroup\$
    – Omer
    May 13, 2018 at 17:42
  • \$\begingroup\$ Whats the error? \$\endgroup\$
    – John Smith
    May 13, 2018 at 18:47
  • \$\begingroup\$ Cannot implicitly convert type 'void' to 'UnityEngine.Quaternion' \$\endgroup\$
    – Omer
    May 14, 2018 at 3:07
  • \$\begingroup\$ Perhaps try adding (Quaternion) before Chest.LookAt \$\endgroup\$
    – John Smith
    May 14, 2018 at 11:02
  • \$\begingroup\$ I did this but nothing change. Thank you Vadim for all what you did for me. \$\endgroup\$
    – Omer
    May 14, 2018 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .