Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question When I rotate the controls become change depending on the area I'm facing

Discussion in 'Scripting' started by Strico_inc, Sep 27, 2022.

  1. Strico_inc

    Strico_inc

    Joined:
    Sep 27, 2022
    Posts:
    1
    hello, im very new to coding and unity as a whole but I got the camera moving and rotating but the consoles keep changing depending on which axis I'm facing while moving around the scene. So for example, if I'm facing the front of the x axis the controls are normal ( a is left, d is right, etc.) but if I'm facing the back of the y axis everything moves differently to what it should. its almost like the keys don't rotate with the cameras movement.

    this is my code for moving:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class movescript3 : MonoBehaviour
    6. {
    7.     Vector3 Vec;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.         Vec = transform.localPosition;
    19.         Vec.y += Input.GetAxis("Jump") * Time.deltaTime * 20;
    20.         Vec.x += Input.GetAxis("Horizontal") * Time.deltaTime * 20;
    21.         Vec.z += Input.GetAxis("Vertical") * Time.deltaTime * 20;
    22.         transform.localPosition = Vec;
    23.     }
    24. }
    25.  
    this is my code for looking around:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Mouse_Look : MonoBehaviour
    6. {
    7.  
    8.     private Vector2 rotation = Vector2.zero;
    9.     public float speed = 3;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         rotation.y += Input.GetAxis("Mouse X");
    20.         rotation.x += -Input.GetAxis("Mouse Y");
    21.         transform.eulerAngles = (Vector2)rotation * speed;
    22.  
    23.     }
    24. }
    25.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,567
    You must rotate the inputs by the camera heading.

    This usually involves multiplying by a Quaternion. See lines 124 to line 130 here:

    https://github.com/kurtdekker/proxi...edControls/RotatedControlsPlayerController.cs

    Alternately, here is a full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
unityunity