Hey, I’m working on my first Unity project and ran into an issue setting up movement controls for my 3D player character. I’m very new to programming and C# alike, so I used a tutorial to do this and (aside from renaming the variables) followed it to a T. Everything works as intended, except for the movement direction of the character. The movement direction remains the same regardless of the way the camera is facing, in what I assume are the default angles of the world. In other words, when I press “W” the character always moves in the reverse direction of where he was initially facing, even if I’m looking elsewhere. No one in the comments of the tutorial video mentioned this issue, and in the tutorial itself the movement directions worked fine. As such, I tried several other tutorials and googling as well as some clueless attempts of my own, but the same issue remained. If I understand the code correctly, I assume the problem is that “transform” isn’t picking up the rotation of the character object. The current, cobbled-together version of the script is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Charmove : MonoBehaviour
{
CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Cameralook cameraLook;
public Transform cam;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
characterController = GetComponent<CharacterController>();
cameraLook = GetComponent<Cameralook>();
}
void Update()
{
MovePlayer();
}
private void MovePlayer()
{
float horInput = Input.GetAxis("Horizontal") * speed;
float verInput = Input.GetAxis("Vertical") * speed;
Vector3 camF = cam.forward;
Vector3 camR = cam.right;
camF.y = 0;
camR.y = 0;
camF = camF.normalized;
camR = camR.normalized;
transform.position += (camF * verInput + camR * horInput);
if (characterController.isGrounded)
{
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
}
}
The object I’m using for the player character is a Blender model, in case that matters at all. It’s well possible I’ve messed something up on Blender’s end too, since I’m new to that as well. I’ll provide more info if necessary, just not sure what to include and what not to right now. Thanks in advance for any help, I’m personally at my wits’ end.