Hello everyone!
So, i wanted to make a 3rd person movement controller and i watched Brackeys video about it. But there was no jumping. So i tried to make it myself and got stuck here:
So, the broblem is - if i add “Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;” i can’t jump any more, but if i remove it, i can jump but can’t walk the way my camera is looking. Any suggestions how can i fix it?
thx!
public class NewPlayerController : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public float gravityValue = 9.81f;
public float moveSpeed = 10f;
public float jumpSpeed = 3.5f;
float dirY;
bool allowToJump = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
allowToJump = true;
}
float horizontalInput = Input.GetAxis(“Horizontal”);
float verticalInput = Input.GetAxis(“Vertical”);
Vector3 direction = new Vector3(horizontalInput, 0f, verticalInput).normalized;
if (Input.GetButtonDown(“Jump”) && allowToJump)
{
dirY = jumpSpeed;
allowToJump = false;
}
dirY -= gravityValue * Time.deltaTime;
direction.y = dirY;
if (direction.x >= 0.1f || direction.z >= 0.1f || direction.x <= -0.1f || direction.z <= -0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.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.Move(moveDir.normalized * moveSpeed * Time.deltaTime);
}
}
}