x


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 :

  1. rotation doesnt work well and it speeds up !!
  2. 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;  
}
more ▼

asked Aug 11 '12 at 12:46 PM

shahinexir gravatar image

shahinexir
0 4 8 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);

more ▼

answered Aug 11 '12 at 01:04 PM

Brett_MMU gravatar image

Brett_MMU
72 2 2 3

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 ! :)

Aug 11 '12 at 01:52 PM shahinexir

yes, you need to remove transform.Translate(Vector3.right ); from your rotation section, so make it look like this-->

  if (Input.GetKey("left") || Input.GetKey("a"))
  {
    this.gameObject.transform.rotation = Quaternion.Euler(0,180,0);
  }   

also you can remove this.gameObject from before your transforms

  if (Input.GetKey("left") || Input.GetKey("a"))
  {
    transform.rotation = Quaternion.Euler(0,180,0);
  }  
Aug 11 '12 at 02:02 PM Brett_MMU

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 ;)

Aug 11 '12 at 02:43 PM shahinexir

replace

amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;

with

amtToMove = playerSpeed * Input.GetAxisRaw("Horizontal") * Time.deltaTime;

Aug 11 '12 at 02:49 PM Brett_MMU

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 :(

Aug 11 '12 at 03:04 PM shahinexir
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3129
x2247
x1071
x747
x9

asked: Aug 11 '12 at 12:46 PM

Seen: 402 times

Last Updated: Aug 11 '12 at 03:04 PM