- Home /
Trying to get character to instantly rotate in 2.5d side scroller
So I need the character to "flip" sides whenever the mouse is on the left or right of the screen. I'd like to say I'm very new to scripting and only know the basics, which is part of my problem. I figured if I could right a true false statement that involved the Mouse X input then I could just flip the object whenever the mouse x value was below 0. The error comes in the line where I'm trying to do the rotation.
Hopefully I'm making some sense
var lookForward : boolean = true;
function Update () {
if(Input.GetAxis("Mouse X") < 0) {
lookForward = false;
}
if(!lookForward) {
transform.Rotate.Vector3(0, -90, 0 * Time.deltaTime);
}
}
Answer by aldonaletto · Nov 22, 2012 at 03:05 AM
transform.Rotate.Vector3 doesn't exist: you should write transform.Rotate(Vector3(...));
Anyway, the code you're using turns the character -90 degrees each frame the mouse is moving to the right. In order to flip the character left/right when the mouse is on the left/right sides of the screen, use this:
private var lookLeft: Quaternion;
private var lookRight: Quaternion;
function Start(){
// assuming character starts looking to the right:
lookRight = transform.rotation;
// calculate rotation flipped 180 degrees:
lookLeft = lookRight * Quaternion.Euler(0, 180, 0);
}
function Update(){
// if mouse is on the left side of the screen...
if (Input.mousePosition.x < Screen.width/2){
transform.rotation = lookLeft; // look left
} else {
transform.rotation = lookRight; // look right
}
}
Thanks a bunch! I actually figured it out and got it working I was so proud looking up that input mouse position and dividing the screen width by 2! In all seriousness I wrote a function similar to yours, although I think yours is definitely more efficient. Thanks again.
Worked for me as well. Looked for this forever! Thanks you very much!
Your answer
Follow this Question
Related Questions
how to do 2.5D side scroller-Gun point towards mouse then -180 character turn 1 Answer
Side Scroller Script, Need help 1 Answer
character turning around. 3 Answers
Any good NEW 2D side scrolling tutorials? -1 Answers
how to get a camera to side scroll 1 Answer