- Home /
Weird Behaviour with brackeys third person tutorial
Hi!
I followed Brackey's Third Person Controller tutorial perfectly and indeed everything is mostly working.
I noticed a strange behavior where when turning to left and moving the camera at the same time (Like strafing sideways while pointing to a target) the character will spin forever.
I managed to narrow the problem to some type of issue with Math, SmoothDamp and the way Unity deals with angles, because only turning left and sometimes backpedaling has this issue, turning right and walking forward works perfectly 100% of the time.
Can someone help me fix this?
Sure!
Also, an update: The problem is less frequent since I wrote a method that gets an angle and constrains it to the -180 to 180 range , for example an input angle of 225 returns -135.
I still get spins, but usually one or two spins and not infinite spins, but I still would like to know what is happening.
void Update()
{
//jump
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, ground$$anonymous$$ask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = $$anonymous$$athf.Sqrt(jumpHeight * -2 * gravity);
}
//gravity
velocity.y += gravity * Time.deltaTime;
controller.$$anonymous$$ove(velocity * Time.deltaTime);
//walk
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = $$anonymous$$athf.Atan2(direction.x, direction.z) * $$anonymous$$athf.Rad2Deg + cam.eulerAngles.y;
float angle = $$anonymous$$athf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.$$anonymous$$ove(moveDir.normalized * speed * Time.deltaTime);
}
}
Answer by Shiruba · Dec 10, 2020 at 01:25 PM
I'm using 2020.1.3f1 Tutorial is 2019.3.9f1
Will try to mess around with magnitude, thanks.
OK GOT IT
The problems comes from the use of SmoothDamp to smooth the angles, the math function has no idea that angles after 180 should be -180 and so on, everytime the function has to smooth between positive and negative values, it "turns around" and takes the long way to the desired number.
Using Quaternion.Lerp solved everything!
Answer by logicandchaos · Dec 10, 2020 at 12:07 PM
You can try adjusting this value: if(direction.magnitude >= 0.1f) try making it .25f and other values see if it helps. What version of unity was used in the tutorial and what version are you using?