Search Unity

Movement and Animation while Locking Target

Discussion in 'Scripting' started by meganinja, Nov 27, 2021.

  1. meganinja

    meganinja

    Joined:
    Apr 2, 2015
    Posts:
    35
    Hello Everyone

    I'm trying to make a Tennis Game, where the camera is fixed and the character is ALWAYS facing the ball.
    For that reason, I need him to animate correctly according to which direction he is facing. Since the camera is fixed, pressing D should make him go to the RIGHT. IF he is in the blue line, he needs to make a strafe animation. If he is in the red line, he should make the animation of going forward or backward.

    upload_2021-11-27_12-2-7.png

    Here is a video for better understanding and showing my progress:


    I am using a blend tree and sending the rigidbody velocity (X and Z) as parameters.
    upload_2021-11-27_12-5-57.png

    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         //MOVE PLAYER
    4.         rb.AddForce(direction * speed * 500 * Time.deltaTime);
    5.         rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxAcceleration);
    6.  
    7.         //SET ANIMATOR PARAMETERS
    8.         Vector3 animTree = new Vector3(rb.velocity.x, 0, rb.velocity.z);
    9.         anim.SetFloat("X", animTree.x);
    10.         anim.SetFloat("Y", animTree.z);
    11.  
    12.  
    13.     }
    14.  
    15.     private void LateUpdate()
    16.     {
    17.         //ROTATE OBJECT FO BALL DIRECTION
    18.         if (ball)
    19.         {
    20.             var lookPos = BallDirection();
    21.             lookPos.y = 0;
    22.             var rotation = Quaternion.LookRotation(lookPos);
    23.             rb.MoveRotation(Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * angularDamp));
    24.         }
    25.     }
    Does anyone have an Idea on how to make it? Maybe there is a way of getting the right Vector out of all of this to send to the Animator. Or maybe some math that I don't know about it?
     

    Attached Files:

  2. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    Code (CSharp):
    1.  
    2. Vector3 adjustedTargetPosition = target.transform.position;
    3. Vector3 rot = adjustedTargetPosition - this.transform.position;
    4. Quaternion newRotation = Quaternion.LookRotation(rot);
    5. rb.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 1500f);
    6. Vector3 vel = cam.transform.forward * Input.GetAxis("Vertical") + cam.transform.right * Input.GetAxis("Horizontal");
    7. Vector3 localVel = this.transform.InverseTransformDirection(vel);
    8. thisAnimator.SetFloat("LocalVelZ", localVel.z, .1f, Time.deltaTime);
    9. thisAnimator.SetFloat("LocalVelX", localVel.x, .1f, Time.deltaTime);
    i dunno, maybe something like this? you'll probably have to play around with it.
     
  3. meganinja

    meganinja

    Joined:
    Apr 2, 2015
    Posts:
    35
    Hey Anthony, thanks for the tip. I will try to use this code somehow. Do some experiments.

    I'll be back soon with the results.
     
  4. patihan70

    patihan70

    Joined:
    Oct 10, 2022
    Posts:
    2
    have you already solved this problem? I have struggled with this problem too.