x


Moving freely on a 2D plane

Hello, I have a question about controlling and object. I want to use WASD to control an object from a 2D perspective (so when W is pressed the object moves up, S down, etc.), I know how to position the camera in the right place, but how would I control the movement?

more ▼

asked Nov 07 '10 at 04:21 AM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

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

2 answers: sort voted first

This will move an object depending on its rotation:

float Speed = 15;

if(Inptu.GetKey(KeyCode.W)
   transform.position += transform.forward * Speed; //or transform.up * Speed
else if(Inptu.GetKey(KeyCode.S)
   transform.position += transform.forward * -Speed; //or transform.up * -Speed

if(Inptu.GetKey(KeyCode.A)
   transform.position += transform.right * -Speed;
else if(Inptu.GetKey(KeyCode.D)
   transform.position += transform.right * Speed;
more ▼

answered Nov 07 '10 at 11:45 AM

IJM gravatar image

IJM
1.4k 2 5 19

I am somewhat new to unity, where would I put this function inside of a script?

Nov 07 '10 at 05:42 PM Tyler 2

Create a new C# script and put this into the Update() function.

Nov 08 '10 at 05:12 AM IJM
(comments are locked)
10|3000 characters needed characters left

you could also add the first person prefab controller to your scene and disable the mouselook script. then place your camera wherever you want and hit play. the camera will remain in the same position, based on weather or not you move the character using awsd. hope this helps.

shawn

more ▼

answered Mar 16 '11 at 01:40 PM

shawnkilian gravatar image

shawnkilian
361 21 28 41

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

x1364
x1033
x223

asked: Nov 07 '10 at 04:21 AM

Seen: 1933 times

Last Updated: Nov 07 '10 at 04:21 AM