- Home /
Question by
Salja · Dec 07, 2020 at 11:26 AM ·
movementanglecalculation
Worng SignedAngle
Hello everyone, I have a little problem and unfortunately I can't go on on my own. I'm currently working on a character controller and I want to calculate the angle while walking (-180, -90, 0, 90, 180) The whole thing works even if I run normally and am not in strafe mode, but as soon as I go into the strafe mode I get strange values (-0.3694153, -2.219208 -21.25256) Unfortunately, I don't get the values like without a strafe.
Because my English is not that good, I made another video https://youtu.be/MnsqcMp86zk
Here the code
private void Update() {
inputX = Input.GetAxisRaw("Horizontal");
inputZ = Input.GetAxisRaw("Vertical");
direction = new Vector3(inputX, 0, inputZ);
direction.Normalize();
// Check if we have movement input
isMoving = direction != Vector3.zero;
// If the movement is disabled return Vector.zero
direction = playerInput.isMovementEnabled ? direction : Vector3.zero;
inputMagnitude = direction.magnitude;
inputMagnitude *= speedFactor;
// Change MoveType if Player is in Stance to Strafe
moveMode = stance == Stances.OneHandSword ? MoveMode.Strafe : MoveMode.Directional;
if (playerInput.isRotationEnabled && isMoving) {
// Normal Movement none Strafe
if (moveMode == MoveMode.Directional) {
targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothDamp);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
}
// Strafe Movement
if (moveMode == MoveMode.Strafe) {
targetAngle = cameraTransform.eulerAngles.y;
angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothDamp);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
}
}
Vector3 forwardA = cameraTransform.rotation * Vector3.forward;
Vector3 forwardB = transform.rotation * Vector3.forward;
// get a numeric angle for each vector, on the X-Z plane (relative to world forward)
float angleA = Mathf.Atan2(forwardA.x, forwardA.z) * Mathf.Rad2Deg;
float angleB = Mathf.Atan2(forwardB.x, forwardB.z) * Mathf.Rad2Deg;
// get the signed difference in these angles
float _signedAngle = Mathf.DeltaAngle(angleA, angleB);
Debug.Log(_signedAngle);
// Update Animator
animator.SetFloat(HashHorizontal, direction.x, speedSmoothDamp, Time.deltaTime);
animator.SetFloat(HashVertical, direction.z, speedSmoothDamp, Time.deltaTime);
animator.SetFloat(HashInputMagnitude, inputMagnitude, speedSmoothDamp, Time.deltaTime);
animator.SetInteger(HashStance, (int)stance);
animator.SetBool(HashIsGrounded, isGrounded);
animator.SetFloat(HashFalltime, lastFalltime);
animator.SetBool(HashIsCrouching, isCrouching);
animator.SetBool(HashIsDead, isDead);
playerInput.UpdateInput();
// Load ScriptableAbility on Update
for (int i = 0; i < abilities.Count; i++) {
if (abilities[i].IsEnabled) {
abilities[i].OnCharacterUpdate();
}
}
}
screenshot-4.png
(73.1 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613021218im_/https://answers.unity.com/themes/thub/images/avi.jpg)