- Home /
Question by
TideBoi · Aug 03, 2020 at 05:29 PM ·
movementmovement scriptthird-personthird person controllerthirdpersoncontroller
Why does my character constantly move forward and also at the start jump up into the air in a way?,Why does my character constantly go forward and goes up when starting ?
my character goes forward constantly and slightly goes up when i start my project in the game tab im using a third person movement script from this YouTube video: https://www.youtube.com/watch?v=4HpC--2iowE
I am not very good at coding at all and have almost no experience so sorry if i made a stupid mistake here is the code i got in the end: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour {
public CharacterController controller;
public float speed = 250f;
public Transform cam;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, -9.8f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(current: transform.eulerAngles.y, targetAngle, currentVelocity: ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized *speed * Time.deltaTime);
}
}
}
Comment