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. 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. Code (CSharp): private void FixedUpdate() { //MOVE PLAYER rb.AddForce(direction * speed * 500 * Time.deltaTime); rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxAcceleration); //SET ANIMATOR PARAMETERS Vector3 animTree = new Vector3(rb.velocity.x, 0, rb.velocity.z); anim.SetFloat("X", animTree.x); anim.SetFloat("Y", animTree.z); } private void LateUpdate() { //ROTATE OBJECT FO BALL DIRECTION if (ball) { var lookPos = BallDirection(); lookPos.y = 0; var rotation = Quaternion.LookRotation(lookPos); rb.MoveRotation(Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * angularDamp)); } } 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?
Code (CSharp): Vector3 adjustedTargetPosition = target.transform.position; Vector3 rot = adjustedTargetPosition - this.transform.position; Quaternion newRotation = Quaternion.LookRotation(rot); rb.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 1500f); Vector3 vel = cam.transform.forward * Input.GetAxis("Vertical") + cam.transform.right * Input.GetAxis("Horizontal"); Vector3 localVel = this.transform.InverseTransformDirection(vel); thisAnimator.SetFloat("LocalVelZ", localVel.z, .1f, Time.deltaTime); thisAnimator.SetFloat("LocalVelX", localVel.x, .1f, Time.deltaTime); i dunno, maybe something like this? you'll probably have to play around with it.
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.