So, first of all, I am pretty much brand new to programming in c#, and have never done so before. I can’t read just any code though. I have a hard time reading my own code, so please try to make the answer as user friendly as possible. : )
My problem is that I want the direction you are moving the player in to change when you rotate the camera around him. If I hold W down to keep my character moving forward and rotate the camera, he keeps following in the same direction. I cant make him switch directions as I turn the camera. Also, if you put this code into a capsule, you will see that the rotation direction changes after you finish moving, so it looks really bad. The effect is especially worse when you use a custom skin like the one I did. I had a face on him, and he faces the direction he’s moving. but when he stops moving, he rotates back to his original position. Also, my character was a capsule, so if you copy the code exactly, you can see all these problems for yourself
I am not sure if this is useful, because it could be that the code I need is for the camera, but I don’t have any scripts for the camera, I am using Cinemachine. So, here’s the c# for the character: (P.S: sorry, I couldn’t figure out how to make that using different stuff piece of the code show in the code box, hence all of the slashes… after the slashes end, the code starts
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
using System.Collections;
using System.Collections.Generic;
using UnityEditor.UIElements;
using UnityEngine;
public class control : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public CharacterController controller;
private Vector3 moveDirection;
public float gravityScale;
void Start()
{
Cursor.visible = false;
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0f, Input.GetAxis("Vertical") * moveSpeed);
transform.rotation = Quaternion.LookRotation(moveDirection);
transform.rotation *= Quaternion.Euler(0f,90f,0f);
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
else {
moveDirection.x = Input.GetAxis("Horizontal") * moveSpeed/2;
moveDirection.z = Input.GetAxis("Vertical") * moveSpeed/2;
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
}
}