x


TopDown Controls - World Movement

Hello,

I'm working on a top down controller for my character. Right now, it rotates towards where the mouse is on the screen, and when you press 'W', or UP it will move forward in the direction of the mouse. 'S' or DOWN, will move away and so on.

This works fine, but I also want an option so I can change the movement to world directions, what I mean by that, is no matter what rotation the character is at, 'W' or UP will always move Up/North/+Z, and the same with all the other directions. For some reason, I really cant seem to get my head around this?

Below is my script for moving the character in local position, with rotation of the mouse:

// ----- Move Character ----- \\
    //move in local position
    if(!allowWorldMovement){
       if(allowRotateStatic){
         //rotate player to face direction arrow
         player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
       }
       if(Input.GetAxis("Vertical")){
         //rotate player to face direction arrow
         player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);

            //them move player towards arrow direction
          var translation : float = Input.GetAxis("Vertical") * playerMoveSpeed;
           translation *= Time.deltaTime;

         player.transform.Translate(0,0,translation);
       }
       if(Input.GetAxis("Horizontal")){

         var panning : float = Input.GetAxis ("Horizontal") * playerPanSpeed;
         panning *= Time.deltaTime;

         player.transform.Translate(panning,0,0);

         //pan the character left/right whilst looking at mouse position/direction arrow
         if(allowRotatePan){
          player.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
         }
       }
    }

And then in the world position, I'm not making any progress, my character 'tries' to move, but seems like hes stuck, and then just bounces back to his original position?

//move in world space
    else{

       var translationW : float = Input.GetAxis ("Vertical") * 10;
       translationW *= Time.deltaTime;

       //if(Input.GetAxis("Vertical")){
         player.transform.position = Vector3(0, 0, translationW);
       //}
    }
more ▼

asked May 21 '12 at 05:16 PM

oliver-jones gravatar image

oliver-jones
2.5k 206 226 254

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

1 answer: sort voted first

It's quite simple. You just use position.x or position.z.

ExampleScript:

if(Input.GetButton("Test"))
gameObject.transform.position.x+=2*Time.deltaTime;

This makes the gameobject move along the worlds x-axis and it does not depend on its angle.

It think for example Vector3(2,0,0) also works just fine.

Hope this helps.

more ▼

answered May 22 '12 at 03:53 PM

ExTheSea gravatar image

ExTheSea
2k 12 23 30

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

x1047
x886
x224
x109

asked: May 21 '12 at 05:16 PM

Seen: 581 times

Last Updated: May 22 '12 at 03:53 PM