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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

when it turns it reset back to the original rotation this is my code

Discussion in 'Scripting' started by Noahthegen, Aug 29, 2021.

  1. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class Camera : MonoBehaviour
    7. {
    8. public float lookSenesitivity = 5;
    9. private Vector2 lookVector;
    10. private Vector3 rotation;
    11. private Vector2 moveVector;
    12.  
    13. // Start is called before the first frame update
    14. void Start()
    15. {
    16.  
    17. }
    18.  
    19. // Update is called once per frame
    20. void Update()
    21. {
    22. Rotate();
    23. }
    24. public void onLook(InputAction.CallbackContext context)
    25. {
    26. lookVector = context.ReadValue<Vector2>();
    27. }
    28.  
    29. private void Rotate()
    30. {
    31. rotation.y = lookVector.x*lookSenesitivity*Time.deltaTime;
    32. transform.localEulerAngles = rotation;
    33. }
    34. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    You are ALWAYS going to have a tough time using EulerAngles directly.

    The reason is they are ambiguous.

    You are also ALWAYS going to have a tough time if you name your scripts the same as existing UnityEngine namespace classes, in this case
    Camera
    .

    While not technically illegal, you do have to make a lot of extra accommodations ALL OVER your code if you insist on such redundant confusing naming.

    Just don't make life hard for yourself: choose a better name.

    Finally, if all you're doing is yet another camera controller, since camera stuff is actually really quite tricky, it might be best to use Cinemachine from the Unity Package Manager.
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,303
    Your main problem is that you are not tracking the rotation correctly


    Code (CSharp):
    1. lookVector.x*lookSenesitivity*Time.deltaTime;
    This is the CHANGE in rotation since the last frame.

    So when you say
    Code (CSharp):
    1. rotation.y = lookVector.x*lookSenesitivity*Time.deltaTime;
    You're assigning the change to a vector y, and overwriting what happened previously

    To track it, you would need to add the change, not overwrite the value
    Code (CSharp):
    1. rotation.y += lookVector.x*lookSenesitivity;
    You would really only need to track the rotation for a specific reason though, like if you are clamping the rotation within a range on that axis for example, which is exactly what people are doing with the vertical axis when they go on to assign the value directly to transform.localEulerAngles. Using this won't be a problem if you are clamping the rotation between 90 and -90, because the euler angles will never get to the point where they flip and cause chaos.

    If you don't need to clamp it (usually you don't clamp the horizontal rotation), then you can directly use the change in rotation and just apply the change to the current rotation without tracking it and without using euler angles. A camera that isn't clamped on the vertial axis will be able to flip over, like a plane doing a loop.

    rotate on the horizontal axis
    Code (CSharp):
    1. transform.Rotate(Vector3.up * lookVector.x * lookSenesitivity);
    I think input vector is already a delta, so I don't think you don't need deltatime, but don't quote me on that.

    Really you would want to add smoothing as well, because the mouselook you are going to get is going to feel weird, too snappy, too precise, not what you're used to.


    I'm really just explaining this in case you are creating a camera to learn how, or you are creating a character controller.
    If you are looking for an actual camera, cinemachine is probably a good choice
     
    Last edited: Aug 29, 2021
  4. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    Thanks fixed it:D