-1
\$\begingroup\$

In my code I play animation that make the player walking backward. Once the player is walking backward I want it to reach a target but not to be facing the target but to keep walking backward to the target.

In the Update()

private void Update()
    {
        var distance = Vector3.Distance(crate.position, player.position);
        if (distance < 1.7f && crateOpenOnce == false && unlockCrate.HasOpened())
        {
            rb.isKinematic = false;
            crateOpenOnce = true;
        }

        if(startSpeedUp)
        {
            animator.Play("Walk Backward");
            SpeedUp();

            float step = movementSpeed * Time.deltaTime;
            player.position = Vector3.MoveTowards(player.position, target.position, step);
        }
    }

The Vector3.MoveTowards doesn't seems to be working not affecting the player he keep walking backward straight.

"Walk Backward" is the animation and inside SpeedUp I slowly increasing the speed.

The "Walk Backward" inside the SpeedUp() is a float parameter from the Animator Controller I might have to give it some other name.

private void SpeedUp()
    {
        if (timeElapsed < lerpDuration)
        {
            timeElapsed += Time.deltaTime;
            valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
            animator.SetFloat("Walk Backward", valueToLerp);
        }
    }

The player is start walking slowly and speeding up backward while his head is watching another object that move both are moving at the same time :

Player is walking backward

The player is walking backward and looking at the rolling object and you can see on the left a bit top a very small cube and this is should be the target where the player should move to.

The player should not rotating facing the cube but moving to the cube while he is moving backward also to move to the cube but the player is keep moving backward and never getting close to the cube.

Updating about what I tried :

Because I'm adding speed to the backward animation I don't want to MoveTowards the player to the target but to RotateTowards the target :

private void Update()
    {
        var distance = Vector3.Distance(crate.position, player.position);
        if (distance < 1.7f && crateOpenOnce == false && unlockCrate.HasOpened())
        {
            rb.isKinematic = false;
            crateOpenOnce = true;
        }

        if(startSpeedUp)
        {
            animator.Play("Walk Backward");
            SpeedUp();

            
            Vector3 targetDirection = target.position - player.position;
            // The step size is equal to speed times frame time.
            float singleStep = playerRotatingSpeed * Time.deltaTime;
            // Rotate the forward vector towards the target direction by one step
            Vector3 newDirection = Vector3.RotateTowards(player.forward, targetDirection, singleStep, 0.0f);
            player.rotation = Quaternion.LookRotation(newDirection);
        }
    }

The problem now is that the player is rotating facing the target and I don't want it to face the target but to keep walking backward to the target the rotation should be maybe only on the Y.

This line make the player facing the target and I don't want it ! I don't want that behaviour.

player.rotation = Quaternion.LookRotation(newDirection);
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Your title currently reads like "How do I do X without doing X," which is very confusing. Do you mean to ask "how can I rotate the player to face away from a target" or "how can I rotate the player to turn their back to a target" or "how can I rotate the player's head to face a target without rotating their body"? Or something else? \$\endgroup\$
    – DMGregory
    Aug 1 at 3:37
  • \$\begingroup\$ You right the question supposed to be how to rotate the back to the target. \$\endgroup\$ Aug 1 at 6:15
  • \$\begingroup\$ Try editing your question then rather than just posting a comment. \$\endgroup\$
    – DMGregory
    Aug 31 at 11:30
0
\$\begingroup\$

The answer at least it's working for me :

To add minus to the lookPos :

if(startSpeedUp)
        {
            animator.Play("Walk Backward");
            SpeedUp();

            var lookPos = target.position - player.position;
            lookPos.y = 0;
            var rotation = Quaternion.LookRotation(-lookPos);
            player.rotation = Quaternion.Slerp(player.rotation, rotation, Time.deltaTime * rotationSpeed);

        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.