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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Leaning/peeking left and right snaps my camera to the Z axis.

Discussion in 'Scripting' started by Frilent, Sep 18, 2020.

  1. Frilent

    Frilent

    Joined:
    Aug 26, 2020
    Posts:
    2
    Hey guys, I'm fairly new to both Unity and scripting and ran into a small issue. I'm working on a first person character controller and everything is working fine, but whenever I lean left or right my player snaps to facing the Z axis and then performs the lean. I'm trying to get my player to lean left and right without the camera snapping like that.

    The code below is attached to my main camera which is a child of the player.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Leaning : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.  
    10.         if(Input.GetKey("e") || Input.GetKey("q"))
    11.         {
    12.             Lean();
    13.         }
    14.      
    15.     }
    16.  
    17.     void Lean()
    18.     {
    19.         if(Input.GetKey("q") == true)
    20.         {
    21.             transform.rotation = Quaternion.Euler(0,0,30);      
    22.         }
    23.         if(Input.GetKey("e") == true)
    24.         {
    25.             transform.rotation = Quaternion.Euler(0,0,-30);
    26.         }
    27.     }
    28. }
    I'm also having the same issue with this blink/teleport script. It works but I only blink on the Z axis going one direction. This is attached to the Player which is the parent of the camera.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Blink : MonoBehaviour
    6. {
    7.     public float distance = 5.0f;
    8.     public CharacterController controller;
    9.  
    10.     void Start()
    11.     {
    12.         controller = GetComponent<CharacterController>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if(Input.GetKeyDown(KeyCode.LeftShift))
    18.         {
    19.             BlinkForward();
    20.         }
    21.     }
    22.  
    23.     public void BlinkForward()
    24.     {
    25.         RaycastHit hit;
    26.         Vector3 destination = transform.position + transform.forward * distance;
    27.  
    28.         //INTERSECTING AN OBJECT
    29.         if(Physics.Linecast (transform.position, destination, out hit))
    30.         {
    31.             controller.Move(destination * distance * Time.deltaTime);
    32.  
    33.             destination = transform.position + transform.forward * (hit.distance - 1f);
    34.         }
    35.  
    36.         //NO OBSTACLES FOUND
    37.         if(Physics.Raycast(destination, -Vector3.up, out hit))
    38.         {
    39.             controller.Move(destination * distance * Time.deltaTime);
    40.  
    41.             destination = hit.point;
    42.             destination.y = 0.5f;
    43.             transform.position = destination;
    44.         }
    45.  
    46.  
    47.     }
    48. }
    I can also post my main movement script if needed.

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Generally you want to insert an extra transform in your hierarchy where you pivot for the lean/peek.

    With that extra "peek" transform, you would set its
    .localRotation
    , NOT its
    .rotation


    In your case you are driving the root
    transform.rotation
    to (0,0,30), which is why you are snapping to north.

    As for the second part, it's unclear what might be wrong except that the transform this script is on might not be pointed the way you expect it. Use lots of Debug.Log() and find out!
     
    chemicalcrux likes this.
  3. Frilent

    Frilent

    Joined:
    Aug 26, 2020
    Posts:
    2

    The .localRotation worked, thank you! I'm gonna continue working on the second part. I noticed now I can teleport in the direction I'm walking, but if I'm standing still I default to teleporting along the Z axis. Maybe it's the same issue for that script too.

    One last thing, when my camera leans/peeks it snaps to the 30 and -30 instantly. Do you know what would need to be changed to make it a gradual movement? I'm assuming I'd need to add a float for lean/peek speed then insert some sort of math that multiplies my speed by the direction I'm rotating.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
  5. yaroslavdavydkin938

    yaroslavdavydkin938

    Joined:
    Sep 17, 2022
    Posts:
    3