[Codeing help] why does my code act weird?

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?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SideMovement : MonoBehaviour
{
    public Transform GunBody;
    public Transform GameCam;
    public Vector3 RightSide,LeftSide;
    public Vector3 PosRightSide,PosLeftSide;
    Vector3 OrgPos,RotOrgPos;

    void Start()
    {
        OrgPos = GunBody.transform.localPosition;
        RotOrgPos = GameCam.transform.localEulerAngles;
    }


    void Update()
    {
        RightLean();
        LeftLean();
    }

    void RightLean()
    {
        if (Input.GetKey(KeyCode.E))
        {
            print("test");
            GunBody.transform.localPosition = RightSide;
            GameCam.transform.localEulerAngles = PosRightSide;
        }
        else
        {
            GunBody.transform.localPosition = OrgPos;
            GameCam.transform.localEulerAngles = RotOrgPos;
        }
       
    }


    void LeftLean()
    {
        if (Input.GetKey(KeyCode.Q))
        {
            GunBody.transform.localPosition = LeftSide;
            GameCam.transform.localEulerAngles = PosLeftSide;
        }
        else
        {
            GunBody.transform.localPosition = OrgPos;
            GameCam.transform.localEulerAngles = RotOrgPos;
        }
       
    }
}

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.

1 Like

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:

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: SmoothMovement.cs · GitHub

1 Like