Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to set an object in the direction of another?

Discussion in 'Community Learning & Teaching' started by Zimaell, May 10, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    328
    it's about turning the bone in the right direction, this direction is set by another object, animation is not applied in the skeleton, that is, it's just a set of objects. In the video,

    I show what I can do, or rather not. as planned.
    I place the guide on the knee, then where I lead it there and the femur should look, but I don’t get exactly what I planned ...

    attaching the script.

    Code (CSharp):
    1. public Transform RightUpperLeg;
    2. public Transform RightLowerLeg;
    3. public Transform PoleRightKnee;
    4. private Quaternion initialRotation;
    5. private Quaternion lookRotation;
    6.  
    7. private void Awake(){
    8.    PoleRightKnee.position = RightLowerLeg.position;
    9.    initialRotation = RightUpperLeg.rotation;
    10.    }
    11.  
    12. private void Update(){
    13.    lookRotation = Quaternion.LookRotation(PoleRightKnee.position - RightUpperLeg.position);
    14.    RightUpperLeg.rotation = lookRotation * initialRotation;
    15.    }