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

[Codeing help] why does my code act weird?

Discussion in 'Scripting' started by ehabgaming1, Aug 16, 2021.

  1. ehabgaming1

    ehabgaming1

    Joined:
    Mar 20, 2018
    Posts:
    9
    I'm trying to make side leaning (like from rainbow six sieges) when the player leans to the left its works but when the player tries to lean to the right its doesn't work even though it uses the same code you got any ideals on how to fix this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SideMovement : MonoBehaviour
    6. {
    7.     public Transform GunBody;
    8.     public Transform GameCam;
    9.     public Vector3 RightSide,LeftSide;
    10.     public Vector3 PosRightSide,PosLeftSide;
    11.     Vector3 OrgPos,RotOrgPos;
    12.  
    13.     void Start()
    14.     {
    15.         OrgPos = GunBody.transform.localPosition;
    16.         RotOrgPos = GameCam.transform.localEulerAngles;
    17.     }
    18.  
    19.  
    20.     void Update()
    21.     {
    22.         RightLean();
    23.         LeftLean();
    24.     }
    25.  
    26.     void RightLean()
    27.     {
    28.         if (Input.GetKey(KeyCode.E))
    29.         {
    30.             print("test");
    31.             GunBody.transform.localPosition = RightSide;
    32.             GameCam.transform.localEulerAngles = PosRightSide;
    33.         }
    34.         else
    35.         {
    36.             GunBody.transform.localPosition = OrgPos;
    37.             GameCam.transform.localEulerAngles = RotOrgPos;
    38.         }
    39.        
    40.     }
    41.  
    42.  
    43.     void LeftLean()
    44.     {
    45.         if (Input.GetKey(KeyCode.Q))
    46.         {
    47.             GunBody.transform.localPosition = LeftSide;
    48.             GameCam.transform.localEulerAngles = PosLeftSide;
    49.         }
    50.         else
    51.         {
    52.             GunBody.transform.localPosition = OrgPos;
    53.             GameCam.transform.localEulerAngles = RotOrgPos;
    54.         }
    55.        
    56.     }
    57. }
    58.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I'm assuming this is the problem. You are running two methods. RightLean, and then LeftLean. Since RightLean runs first, it tries to resolve RightLean first, however, right after LeftLean is run which tells your GunBody and GameCam to reset back to to the OrgPos and RotOrgPos.

    You can check if this is the case by commenting out the Else part of the LeftLean.

    You basically need to change your code so that If E is pressed, LeftLean doesn't run. And honestly I would even say if Q is pressed, don't run RightLean (really no reason to).

    I would even say maybe tracking stuff using a State to determine what lean you are on to determine how you can reset.

    Possibly instead of simply checking if you aren't hitting a button, check when you release a button to determine you should return to the OrgPos/RotOrgPos. So, a user hits E, I lean right. Then when I release E, the Release triggers and returns me back to the center. This would be better also because I wouldn't constantly be setting GunBody and GameCam back to their original position.
     
    Last edited: Aug 16, 2021
    ehabgaming1 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Building upon everything that Brath says above, what you probably also want is for this to happen smoothly rather than jerking all at once.

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     
    Brathnann likes this.