- Home /
2d game,rotation problem...and need some help to prevent the camera from following my player when it rotates
Hi. Im writing a script for my player of a 2d style game, but i have some problems. I want to make a gameplay like super mario, that when i hold left arrow it rotates to the left side and with no extra rotating continues to going on left direction and same for the right direction.
Now i can move left and right. My camera is attached to my player and follows it only on x axis and everything looks good, but when i rotate left and hold left arrow it moves really fast, i tried to add Time.deltaTime
but it didn't changed . Im really confused . I need some help so plssss help me with this rotation problem and also i want my camera to stop rotating when player rotates cuz they are both parented to each other . so we got 2 prob :
rotation doesnt work well and it speeds up !!
need to add something to prevent rotating the camera when the player rotates...
here is my code:
var playerSpeed : int;
var jumpHeight : int;
function Update ()
{
// getting the transform of the main camera and put it in a new var.
var cameraTransform = Camera.main.transform;
// making the camera child of the player .
cameraTransform.parent = transform;
amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
transform.Translate(Vector3.right * amtToMove);
//------------------------------------------------------------------------
//3- player rotation...!
if (Input.GetKey("left") || Input.GetKey("a"))
{
this.gameObject.transform.rotation = Quaternion.Euler(0,180,0);
transform.Translate(Vector3.right );
}
if (Input.GetKey("right") || Input.GetKey("d"))
{
this.gameObject.transform.rotation = Quaternion.Euler(0,0,0);
}
//--------------------------------------------------------------------------
//4- player jump
// player can countinusly jump when you hold the up button. if its transform position in y axis is equal to 0.73(on the ground)
if ( (Input.GetKey("up") || Input.GetKey("w") ) && transform.position.y == 0.73)
{
amtToJump = jumpHeight + Time.smoothDeltaTime ;
transform.Translate(Vector3.up * amtToJump );
}
}
//5- camera follow
// this function lets the camera be parented to the player but prevent it from moving along the y axis when the player jumps.
function LateUpdate()
{
Camera.main.transform.position.y = 2.77;
}
Answer by Brett_MMU · Aug 11, 2012 at 01:04 PM
on your player rotation code you have "transform.Translate(Vector3.right );" when the left key is pressed down, this is being added to the transform.Translate that occurred in your movement code, as that's what's causing it to speed up. Also in your rotation code I would change it to Input.GetKeyDown() rather than Input.GetKey().
As for the camera, I wouldn't bother parenting it to your player, for 2D platformers I make a seperate script for the camera and have something like
transform.position = new Vector 3(GameObject.FindGameObjectWithTag("Player").transform.position.x, yOffset, zOffset);
oh i really apreciate your help man.tnx alot. the camera code works awsome.and its ok now ;) about the input part , ive tried it befor but it doesnt work well.when i change it to input.getkeydown() its ok when im moving to the right direction.but if i hold the left arrow it wont work and it moves in wrong direction.getkey is better i think ;)
but about the first part.i mean the reason of speeding up when i move to the left direction,im sorry i couldnt get what u mean.u mean that i shoud remove that transform.translate section? but it doesnt work.if u can tell me what to do and help me, i really apreciate it ;) thats the only problem left now . tnx again and forgive me for my bad english ! :)
yes, you need to remove transform.Translate(Vector3.right ); from your rotation section, so make it look like this-->
if (Input.Get$$anonymous$$ey("left") || Input.Get$$anonymous$$ey("a"))
{
this.gameObject.transform.rotation = Quaternion.Euler(0,180,0);
}
also you can remove this.gameObject from before your transforms
if (Input.Get$$anonymous$$ey("left") || Input.Get$$anonymous$$ey("a"))
{
transform.rotation = Quaternion.Euler(0,180,0);
}
aha , tnx again . changed it and the speed problem is done and it moves smoothly . but in the oposite direction! again a new peoblem :( i mean , when i hold left arrow it rotates but it moves to right!!! but the right one is ok ;)
replace
amtTo$$anonymous$$ove = playerSpeed Input.GetAxis("Horizontal") Time.deltaTime;
with
amtTo$$anonymous$$ove = playerSpeed Input.GetAxisRaw("Horizontal") Time.deltaTime;
still the same problem . . . it does not work too :( doesnt matter man . thank for your help . really used alot ;) ill figure it out some how :(