0
\$\begingroup\$

Hi I'm having problem Translating and Positioning at the same time e.g take example of retro spacecraft shooting games in which your spaceship moved in x-axis automatically and you just needed to control position on y-axis. I'm using transform.Translate(new Vector3(0f,Time.deltaTime * speed,0f),Space.World); to translate and then transform.position = new Vector3(Input.mousePosition.x,0f,0f); to position using mouse but it doesn't seem to work. Help !

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Actually what you will find is that the spacecraft does not move at all in retro space shooters; but rather all of the level objects move in the opposite direction instead. i.e. To make it look like the player is moving right; you should make all of the level objects (including the background) move to the left.

So really you shouldn't need to be translating the player on the X axis at all. You should be translating the level objects in the opposite direction on the X-axis instead. This will also make positioning the player, using the mouse position, a lot easier as you can work using absolute screen co-ordinates, without having to worry about the player's world co-ordinates.

EDIT: If you really must move the player's position physically in the world, then I would suggest that you grab the position of the mouse on the screen, then add it to the offset of the view, to set the player's world position correctly, combining them into a single transformation such as:

transform.position = new Vector3(Input.mousePosition.x, _ScreenPosition.Y, 0f);
\$\endgroup\$
1
  • \$\begingroup\$ transform.position = new Vector3(Input.mousePosition.x, Time.time ,0f); works now just need to tweak mouse input like @Tetrad mentioned below. \$\endgroup\$
    – 2600th
    Commented Apr 25, 2012 at 6:06
0
\$\begingroup\$

Both transform.Translate and transform.position affect the same variable. So calling both in the same frame the way you are isn't going to work.

What you want to do is change your mouse input code to take into account the current y and z values of your position.

So do the translate part, and then for setting the x value, do something like this:

transform.position = new Vector3( Input.mousePosition.x, transform.position.y, transform.position.z );

Keep in mind that it's very likely that Input.mousePosition is not what you want. That is in screen coordinate space, not 3D space, so unless your camera is set up to be 1 unit is one pixel, you're likely going to set the X value to something weird. What you might want to do is keep track of the mouse delta, scale it by some appropriate value relative to your world scale, and then just pass that in to the X value for your first transform.Translate.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .