hi guys,
i’m very very new to coding and programing and i’m trying to create TPS movement.
i got the camer and basic controlls from a brackeys video but i can’t add a jump function because every tutorial tells me to use rigidbody, which when added causes the box (player) object to start rolling around when being controlled: instead of ‘gliding’ when i press ‘w’ to go forward it starts rolling.
anyway, here’s the code i used for movement:
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float turnSmoothTime = 0.01f;
float turnSmoothVelocity;
void Update()
{
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 = 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 * speed * Time.deltaTime);
}
}
}
help would be appreciated, even more so thoroughly explained help :)))
I’m certain you’ve missed the step in all of these tutorials where they tell you how to lock the rotation on the rigidbody, which prevents this from happening.
thanks. i’ll try that. if i’m not mistaken that caused it to jitter but i’ll check again.
just out of curiosity, could you explain why my script combined script didn’t work? i got a console error saying that the ‘object reference’ was not set to anything
As a general rule, when you have an error, copy and paste the full error message rather than trying to paraphrase it, as there are details in the message that help to debug it, such as the line number.
Without seeing the line number, the most likely culprit is probably that you need to assign “cam” in the inspector.