Player Camera rotation

Hey,

i am coding a first person controller, but i have the problem that my character doesnt move in camera direction, i tried a lot of stuff and searched for solutions but nothing helped. My camera is parent to my player. Can someone help me and tell what i do wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Rigidbody MyRigidBody;
     [SerializeField] Camera MainCamera;

// MOVE
    [SerializeField] float MoveSpeed;
    [SerializeField] float RotationSpeedH;
    [SerializeField] float RotationSpeedV;

    private float RotationH;
    private float RotationV;


        // JUMP
    [Header("Jump Properties:")]
    [SerializeField] KeyCode Jump = new KeyCode();
    [SerializeField] LayerMask WhatIsGround;
    [SerializeField] Transform FeetPos;
    [SerializeField] float RoundRadius = 1;
    [Range(0, 30)]
    [SerializeField] float JumpForce = 10;


    public bool IsGrounded;
    public bool IsJumping;

    // Start is called before the first frame update
    void Start()
    {
            MyRigidBody = GetComponent<Rigidbody>();
            MainCamera = GetComponent<Camera>();
            FeetPos = GetComponent<Transform>();
             
    }

    // Update is called once per frame
    void Update()
    {
        // mouse rotation
         RotationH += RotationSpeedH * Input.GetAxis("Mouse X");
     
       RotationV -= RotationSpeedV * Input.GetAxis("Mouse Y");

        transform.eulerAngles = new Vector3( 0,  RotationH, 0);

    }

    void FixedUpdate()
    {
        // moving
        Vector3 horizontal = Vector3.right  * Input.GetAxis("Horizontal");
        Vector3 vertical = Vector3.forward * Input.GetAxis("Vertical");

        Vector3 move = (horizontal + vertical) * MoveSpeed;
        MyRigidBody.velocity =  new Vector3(move.x,    MyRigidBody.velocity.y, move.z);


         // jumping
          IsGrounded = Physics.CheckSphere(FeetPos.position, RoundRadius, WhatIsGround);

        if (IsGrounded && (Input.GetKey(Jump)))
        {
            MyRigidBody.AddForce(transform.up * JumpForce, ForceMode.Impulse);  
        }
    }
}

vector3.forward is just a shortform of new Vector3 (0,0,1)

that’s it.

what you’re looking for is transform.forward. Basically gives 1 unit in direction of rotation of gameobject.

1 Like