Let the Player face towards its move direction?
Hello,
At first i had a basic Movementscript on my Player with a Camera following it. But then i made it so, that the Camera looks toward the Center of the Map while following the Player. After i made that change, the Player movement was wrong, because it was relative to the Worldspace and not the Camera itself. But i fixed the problem, so now the movement is working. The only problem i have now is that the player is still facing in the wrong directions because it seems like the direction he is facing towards is still relative to the worldspace. Here is my movement-script:
public class PlayerMovement : MonoBehaviour {
[SerializeField] float walkSpeed = 2f;
[SerializeField] float runSpeed = 4f;
[SerializeField] Transform cameraTransform;
float movementSpeed;
float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
bool walking = false;
public bool isInteracting = false;
Animator animator;
void Start ()
{
animator = GetComponent<Animator>();
}
void Update ()
{
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 inputDir = input.normalized;
Vector2 speed = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick);
if (speed.x > 0.6 || speed.x < -0.6 || speed.y > 0.6 || speed.y < -0.6)
{
walking = false;
movementSpeed = runSpeed;
}
else
{
walking = true;
movementSpeed = walkSpeed;
}
if (!isInteracting)
{
transform.Translate(transform.forward * currentSpeed * Time.deltaTime, cameraTransform);
Vector3 lookRotation = new Vector3(inputDir.x, 0.0f, inputDir.y);
if (lookRotation != Vector3.zero)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookRotation), 0.15F);
currentSpeed = Mathf.SmoothDamp(currentSpeed, movementSpeed * inputDir.magnitude, ref speedSmoothVelocity, speedSmoothTime);
float animationSpeedPercent = ((walking) ? 1f : 2f) * inputDir.magnitude;
animator.SetFloat("Forward", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
}
public void FaceToInteraction(Transform target)
{
Vector3 targetDirection = target.position - transform.position;
transform.rotation = Quaternion.LookRotation(targetDirection);
}
}
Hope you can help. Thanks!
Answer by tormentoarmagedoom · May 07, 2018 at 02:29 PM
Good day.
I did not read the code, but you know there are 2 rotations?
transform.rotation (rotation relative to worldspace)
transform.localRotation (rotation relative to its parent)
MAybe you are using the wrong one in one function.
Bye!
Oh, i just answered you on my other Thread. Thanks again! But that didnt solved my problem ..
Your answer
Follow this Question
Related Questions
Need Help With Having Player look at an object while stationary. 0 Answers
Script help player controller 0 Answers
Help with Vector3.Distance 0 Answers
The referenced script on the behaviour is missing error help plz 2 Answers
help with player movement 0 Answers