crazy leaning issue!

so i am making a lean script for my game…simple right? it seems to work, lets see, ou hit q or e, the camera tilts left or right. oh ok. i see.
but wait… THE CHARACTER ZOOMS ACROSS THE MAP IN THE DIRECTION OPPOSITE THE LEAN!

I mean if i was going to have a fiddler crab as my player character this would be great. but i would like a human as the player… that being said… any ideas???

heres the script:

// leaning code dosent actually lean out, just turns camera

        float targetZ = (transform.rotation.eulerAngles.z > 25f ? 360f : 0f);

        if (Input.GetKey ("q"))
        {
            targetZ = (transform.rotation.eulerAngles.z > 25f ? 360f + leanangle : leanangle);
        }

        else if (Input.GetKey ("e"))
        {
            targetZ = (transform.rotation.eulerAngles.z > 25f ? 360f - leanangle : -leanangle);
        }

        if (transform.localRotation.eulerAngles.z != targetZ)
        {
            float angle = Mathf.Lerp(transform.rotation.eulerAngles.z, targetZ, leanspeed * Time.deltaTime);
            transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, angle);
        }

any advice would be appreciated. i cant figure it out! lol.

…anyone?

…no one? whered the help go on these forums…

Do you have a GIF showing what’s going on?

know how i can do that? lol not too good with video capturing

Nothing here should explicitly move your transform. Start looking at the other scripts attached to the same GameObject. There may be some weird interaction going on.

For example you are tilting a character controller that is using transform.up for gravity, and by rotating it you start falling sideways.

interesting… maybe my movement code?

        //movement
        float forwardspeed = Input.GetAxis("Vertical") * movementspeed;
        float sidespeed = Input.GetAxis("Horizontal") * movementspeed;
      
        verticalVelocity += Physics.gravity.y * Time.deltaTime*2;

How does this code actually touch transform.position?

Use Gifcam.

heres the rest of it, i forgot why but its separated

        float lastHeight = charController.height;
        charController.height = Mathf.Lerp (charController.height, h, Time.deltaTime*7);
        pos.x = Player.position.x;
        pos.y = Player.position.y + (charController.height - lastHeight) / 2;
        pos.z = Player.position.z;
        Player.position = pos;
        Vector3 speed = new Vector3( sidespeed, verticalVelocity, forwardspeed );
       
        speed = transform.rotation * speed;
       
       
        charController.Move ( speed * Time.deltaTime);

maybe its in here

Try playing with + or - on your Player.position values when you’re setting a new Player.position.

Try commenting this out. It seems your speed is directly affected by your rotation. Commenting it out will cause other issues, but if it stops crab movement, then you know where your issue is.

speed = transform.rotation * speed;

that did it. i commented that out, only bad thing was the w,a,s,d controls were backwards/messed up. any ideas on what i can do?

That means you’ll need to play around with + or - in order to “reverse” the “reversed movements” you already have.

Or do (x * -1), where x is your movement values.

ah, ok let me try that.

…yeah i have no idea where to do that… lol

Multiple options. This is where I would start

        Vector3 speed = new Vector3( sidespeed, 0, forwardspeed );
        speed = transform.rotation * speed;
        speed.y = verticalSpeed;
      
        charController.Move ( speed * Time.deltaTime);

Another alternative would be to apply the rotation in your lean script to a different object.

i was going for that, but what other object is there? i want to eventually have it all animated and rigged so in multiplayer, you can see the character lean and have hitboxes, etc

your code snippet worked great! it disabled my crouch,. but it still worked great! heres what ive got:

//movement
        float forwardspeed = Input.GetAxis("Vertical") * movementspeed;
        float sidespeed = Input.GetAxis("Horizontal") * movementspeed;
        verticalVelocity += Physics.gravity.y * Time.deltaTime*2;
        float lastHeight = charController.height;
        charController.height = Mathf.Lerp (charController.height, h, Time.deltaTime*7);
        pos.x = Player.position.x;
        pos.y = Player.position.y + (charController.height - lastHeight) / 2;
        pos.z = Player.position.z;
        Player.position = pos;
        Vector3 speed = new Vector3( sidespeed, 0, forwardspeed );
        speed = transform.rotation * speed;
        speed.y = verticalVelocity;
       
        charController.Move ( speed * Time.deltaTime);

ive refined it and ive got it working perfectly! i disabled the mouse movement while leaning because it causes issues, and i still need to figure out crouching. im using charHeight for it, but it dosent crouch now. any ideas?