Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Ignore this thread, admins feel free to delete. I didn't understand what I was talking about.

Discussion in 'Editor & General Support' started by archlinuxusa, Jan 9, 2022.

  1. archlinuxusa

    archlinuxusa

    Joined:
    Apr 9, 2021
    Posts:
    2
    I am attempting to create a first person view with mouse axis for camera control.
    The Camera is a child of my humanoid and scripted to always keep the character pointing the same direction as itself, this is located under a separate script than my Character Control Script.

    The Camera and Humanoid rotate appropriately however, My movement controls WASD do not rotate their directions when the camera does. This results in confusing directions when the camera has been rotated any direction but forward.

    In my script I am attempting to fetch the forward direction of the camera and set the transform of the character controller to the correct direction - however this does not change anything and results in the same confusing movement directions.

    I do NOT want to use transforms to move my character because I require the character to have a RigidBody. Combining my RidgidBody and Transforms results in horrible movement.

    I would like to keep using CharacterController however I need the WASD keys to operated in the orientation that the player is facing.

    I have referenced Character Controller to no result on this one.


    Below are both of my FirstPersonController and CameraController for reference.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FirstPersonController : MonoBehaviour
    6. {
    7.     public bool isFirstPersonEnabled = true;
    8.     public CharacterController controller;
    9.     private Vector3 playerVelocity;
    10.     private bool groundedPlayer;
    11.     private float playerSpeed = 6.0f;
    12.     private float jumpHeight = 1.0f;
    13.     private float gravityValue = -9.81f;
    14.     public Camera fpvCamera;
    15.  
    16.     void Start()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.        
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.    
    26.      groundedPlayer = controller.isGrounded;
    27.         if (groundedPlayer && playerVelocity.y < 0)
    28.         {
    29.             playerVelocity.y = 0f;
    30.         }
    31.  
    32.         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    33.  
    34.         //Attempt to transform
    35.         controller.transform.TransformDirection(fpvCamera.transform.forward);
    36.  
    37.         controller.Move(move * Time.deltaTime * playerSpeed);
    38.         if (move != Vector3.zero)
    39.         {
    40.             gameObject.transform.forward = move;
    41.         }
    42.  
    43.         // Changes the height position of the player..
    44.         if (Input.GetButtonDown("Jump") && groundedPlayer)
    45.         {
    46.             playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    47.         }
    48.  
    49.         playerVelocity.y += gravityValue * Time.deltaTime;
    50.         controller.Move(playerVelocity * Time.deltaTime);
    51.  
    52.         if(Input.GetKeyDown("escape")){
    53.             Cursor.lockState = CursorLockMode.None;
    54.         }
    55.  
    56.         if(Input.GetKeyDown(KeyCode.LeftShift)){
    57.             playerSpeed = 12.0f;
    58.         }
    59.  
    60.         if(Input.GetKeyUp(KeyCode.LeftShift)){
    61.             playerSpeed = 6.0f;
    62.         }
    63.        
    64.     }
    65. }
    66.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FirstPerson_CameraMouseLook : MonoBehaviour
    6. {
    7.     Vector2 mouseLook;
    8.     Vector2 smoothV;
    9.     public float sensitivity = 5.0f;
    10.     public float smoothing = 2.0f;
    11.    
    12.     public GameObject character;
    13.  
    14.     void Update()
    15.     {
    16.         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    17.         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
    18.         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
    19.         mouseLook += smoothV;
    20.         mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);
    21.         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
    22.         character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
    23.     }
    24. }
    25.  
    TLDR; How do I associate the orientation of the characters direction in the same context as movement buttons.