3D - Move this player lean script (Leaning to the right) to the left. (EASY)
Hello!
I have this older lean script that only works when going to the right. What I want to know, is how I can make it move to the left instead.
So the code below angles/moves/rotates the camera to the right, so the player can see past walls and such. All I need is, it go to the left instead.
Keep in mind it is not the full code, so if you want that to test it out or use the script yourself, please ask.
if(Input.GetKey(KeyCode.E)) {
isLeaning = true;
if(curLean < leanSize) {
Camera.main.transform.Translate(leanSpeed * Time.deltaTime, 0, 0);
if(leanSize > 0.6) {
Camera.main.transform.Rotate(0, 0, leanSpeed * Time.deltaTime * -1 * leanAngle);
}
curLean += leanSpeed * Time.deltaTime;
}
}
if(Input.GetKeyUp(KeyCode.E)) {
isLeaning = false;
}
if(curLean > 0.01 && (!isLeaning)) {
Camera.main.transform.Translate(leanSpeed * Time.deltaTime * -1, 0, 0);
Camera.main.transform.Rotate(0, 0, leanSpeed * Time.deltaTime * 1 * leanAngle);
curLean -= leanSpeed * Time.deltaTime;
}
Did you try reversing Rotate and Translate Vectors?
Camera.main.transform.Translate(-leanSpeed * Time.deltaTime, 0, 0);
Camera.main.transform.Rotate(0, 0, leanSpeed * Time.deltaTime * leanAngle);
If I add what you add it will rotate to the right way, but after I release the key it will move the player to where the camera lean was.
However it does work correctly to the right side, making me think I should add another part of the code that I didnt think was relevant. So could you re-check my code up above. Ill change it now.
Hımm This might do:
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q)) {
isLeaning = true;
if($$anonymous$$athf.Abs(curLean) < leanSize) {
Camera.main.transform.Translate(-leanSpeed * Time.deltaTime, 0, 0);
if(leanSize > 0.6) {
Camera.main.transform.Rotate(0, 0, leanSpeed * Time.deltaTime * leanAngle);
}
curLean -= leanSpeed * Time.deltaTime;
}
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Q)) {
isLeaning = false;
}
if($$anonymous$$athf.Abs(curLean) > 0.01 && (!isLeaning)) {
Camera.main.transform.Translate(leanSpeed * Time.deltaTime * -1, 0, 0);
Camera.main.transform.Rotate(0, 0, leanSpeed * Time.deltaTime * 1 * leanAngle);
curLean -= leanSpeed * Time.deltaTime;
}
That sadly makes it just move forever indefinitely. Isnt it something to do with the if statement I added later on? See$$anonymous$$gly thats what the if statement that runs when you stop leaning.
Answer by CalisIAm · Oct 22, 2015 at 09:52 PM
Got it to work! Thanks! What we missed was to make the leanback if statement be seperate for left and right by making them functions. These functions are now called upon when the E or Q key get UP, so basically when you stop leaning.
This way I could simply change the 1, and -1 around, into -1, and 1 for it to work for the left side.