- Home /
Locking move direction and rotation in JavaScript
Hi all. I'm new to unity and all of my (limited) experience in JavaScript has been either for web development or 2D flash games so I'm having some difficulties with the scripting. I finally finished all the different enemy ai scripts and they seem to be working wonderfully with the exception of one problem: I foolishly programmed them for a 3D environment when it's for a 2D project. The project I am tinkering around with is a top-down vehicle combat game. With this being the case I want my enemies to only move in the X and Z directions and rotate about the Y-axis. Also, a lot of my scripts use "transform.LookAt(target);" This is also a problem as the player and the enemies (through the use of waypoints) are going up and down ramps to different elevations causing enemies to look up and fire at the player. I was hoping for some suggestions for how I could lock the move direction and rotation (preferably through script).
Although, I will say in my defense that when I started programming the AI I didn't know what kind of project I wanted to do. Only after half the scripting was done did I realize I wanted to do a top-down shooter. ^_^''
Sorry for the long question. Any help would be greatly appreciated.
If the characters can go up and down ramps and be at different elevations, etc., isn't it actually a 3-d game, more or less? Or am I misunderstanding?
Technically this is a 3D game. However, with the camera position and player controls it plays more like a 2D game. The main reason there are different elevations is because there are bridge like segments where the player and enemies can either go under or over the bridge. I want to be able to do this without having enemies on the ground being able to look up and fire at the player, especially considering that I have it set up so the player can only fire in the X and Z directions and not up or down.
Answer by Uriel_96 · Nov 20, 2010 at 05:03 PM
for me is more easy to put on the X and Y axis but if you are doing something like he cant go up or down(to use only the X and Y axis) you can use to all that you have something like this:
transform.Translate(transform.forward * Time.deltaTime * speed,Space.World);//or use
transform.Translate(transform.right * Time.deltaTime * speed,Space.World);
or many other several things you can do and for going down or up you only put the gravity(rigidbody). And with this you can use it with several things(using forward or right)or better you can use Vector3.forward or Vector3.right, and for going backwards you use -Vector3.forward and for left use -Vector3.right