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: Code (CSharp): // 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.
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? Code (CSharp): //movement float forwardspeed = Input.GetAxis("Vertical") * movementspeed; float sidespeed = Input.GetAxis("Horizontal") * movementspeed; verticalVelocity += Physics.gravity.y * Time.deltaTime*2;
heres the rest of it, i forgot why but its separated Code (CSharp): 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 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. Code (CSharp): 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.
Multiple options. This is where I would start Code (CSharp): 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: Code (CSharp): //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?