- Home /
Can you help with the character controller
The bottom line is that the character would move around the object with horizontal input. And during the vertical input, the character moved forward.
My code example
Vector3 direction = transform.forward * _moveDirection.y + transform.right * _moveDirection.x;
Vector3 targetDirection = _targetPosition.position - transform.position;
Vector3 rotationDirection = Vector3.RotateTowards(transform.forward, targetDirection, 360, 0.00f);
Quaternion targetRotation = Quaternion.LookRotation(rotationDirection);
float xAngleError = Mathf.DeltaAngle(transform.rotation.eulerAngles.y, targetRotation.eulerAngles.y);
float xTorqueCorrection = _pid.GetOutput(xAngleError, Time.deltaTime);
transform.rotation = Quaternion.Euler(xTorqueCorrection * Vector3.up);
_characterController.Move(direction.normalized * (_walkSpeed * Time.deltaTime));
but due to centrifugal force, the character flies out of the playing field.
Please help me!
Answer by rayith · Feb 20 at 05:10 AM
hay this is how I do first person controller with mouse look
create a empty object (call this your "Player" give it the "Player" tag), add character controller, add the player movement script, add as a child a capsule taller than wide ( offset the "Player" at the top of the capsule ), add a second child with camera and the mouse look script (also place at top of capsule)
PlayerMovement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 5f;
public float gravity = -15f;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
MouseLook:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseXSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseXSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseXSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
hope it helps
avatar image12 hours ago
thank you so much I was really hitting a wall there
ps do you know how to make a inventory system
avatar image4 days ago
nothing happens, the object doesn't move
I tried this script (a modification of something I found online) but still no movement
{
public GameObject objectToMove;
public Vector3 moveDirection;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
objectToMove.transform.position = moveDirection;
}
}
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How do I make my character controller jump when I use this code? 0 Answers
RigidBody immediately stops after AddForce 1 Answer
How to make sure than one script affect the player of the specific client, and not the other 2 Answers
Quick Question About Player Movement And Animations. Please Help! 3 Answers