so i sat down today to learn some Unity and I imported a plane object, rasied the camera above it, attached a CharachterController to the camera object,
and I fiddled around with a script on it. I was able to successfully get gravity to effect the player controller and have it fall and collide with my plane,
from there i was able to get WASD working for basic movement around the plane.
but i seem to have gotten stuck on getting rotation to work correctly using the mouse.
I did some study and what i came across suggested that I should apply my rotation to the top most object in the hierarchy,(which in my case was the camera)
and i can get the camera to rotate, but two problems are becoming evident.
1.there is no limit on the camera rotation and it wiggles and wobbles allover the place.
2. the CharachterController does not rotate with the camera at all. So when i have a 90 degree rotation of the camera, the controlls become rotated 90 degrees as well.
here is my code for my CharachterController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private CharacterController controller;
private Camera localcamera;
private Vector3 playerVelocity;
private bool groundedPlayer;
private float playerSpeed = 2.0f;
private float gravityValue = -9.81f;
[SerializeField] private Transform cameraTransform; // Reference to the camera
private float mouseSensitivity = 100.0f; // Sensitivity for mouse movement
private void Start()
{
controller = GetComponent<CharacterController>();
localcamera = GetComponent<Camera>();
cameraTransform = Camera.main.transform;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// Check if the player is grounded
bool isGrounded = controller.isGrounded;
playerVelocity.y += gravityValue * Time.deltaTime;
if(isGrounded){
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
controller.Move(playerVelocity * Time.deltaTime);
RotateCamera();
}
void RotateCamera()
{
// transform.Rotate(Vector3.up, 50 * Time.deltaTime);
// Get mouse input
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// Vertical rotation (camera rotates on X-axis locally)
localcamera.transform.Rotate(Vector3.up * mouseX);
localcamera.transform.Rotate(Vector3.left * mouseY);
}
}
I had the same problems. Sometimes finding a youtube tutorial is the best idea:
Other than that: its mostly better to use your own movement script than using the character controller (Easier to make the player jump, be affected by gravity…)
yeah so I was genuinely a little confused, I’m working on implementing the above posted example because I was extremely impressed by the results the guy got with it.
but i gotta say it felt kind of “odd” and “clunky” to have like 6 game objects in 2 different heiarchies nested inside each other all moving together instead of using the default character controller element.
I’ve been reading the documentation and i’m not sure i fully understand what im looking at My initial impression was i just had to call the transform and rotation on the highest order element and the properties should just inherit down the tree.
with my first block of code there i was successfully able to get gravity working, Get collisions to stop gravity, get WASD movement to work, and get mouse rotation to move the camera. However there seems to be a mismatch between the camera orientation and the movemnt orientation.
I’m still very much in the “hello world” stage of all this and am learning as im going, but between these two models of having the single charachter controller element with a camera on it.
or
having a rigid body with a movement script, and an empty object to store its transform/orientation inside of it,
and having a seperate object of a Camera holder with a script on update to move its transform position to the position of the transform of the rigidbody object.
which of the two is the better implementation?
my gut says the original one i was doing becuase it is simpler. but at the same time its hard to argue with a successful model like the one presented in the above video
The best implementation is the one you get going that most-closely gives you the result you need.
I’m not trying to dodge the question: the controller for a slow stealth game is going to need different features than one for a high-speed Quake-style shooter.
As you work with the code you create, you will begin to learn how to change it in meaningful ways, bring it closer to the goals you want.
Note: “better implementation” should NOT be a goal.