- Home /
Rotation and Translation problem
So I'm trying to make a Gyruss remake here and I'm working on the movement to get the ship to move in a circular pattern. I've run into a problem, here is an early version of my code.
using UnityEngine;
using System.Collections;
public class PlayerControl : ShipClass
{
public Transform shotStart;
public PlayerShot shot;
void FixedUpdate ()
{
int xAxisValue = (int) Input.GetAxis ("Horizontal");
int yAxisValue = (int) Input.GetAxis ("Vertical");
switch(xAxisValue)
{
case 1:
transform.Rotate (0, 0, p_rotateSpeed);
transform.Translate (1 * p_Speed * Time.deltaTime, 0, 0);
break;
case -1:
transform.Rotate (0, 0, -p_rotateSpeed);
transform.Translate (-1 * p_Speed * Time.deltaTime, 0, 0);
break;
default:
break;
}
switch(yAxisValue)
{
case 1:
transform.Translate (0, -1 * p_Speed * Time.deltaTime, 0);
break;
case -1:
transform.Translate (0, 1 * p_Speed * Time.deltaTime, 0);
break;
default:
break;
}
if (Input.GetButtonDown ("Fire2"))
{
Debug.Log ("Fire2 pressed!");
Instantiate(shot, shotStart.position, shotStart.rotation);
}
/*
*
* Debug Purposes
*
*
if(Input.GetKey("a"))
transform.Translate (-1 * player.p_Speed * Time.deltaTime, 0, 0);
if (Input.GetKey ("d"))
transform.Translate (1 * player.p_Speed * Time.deltaTime, 0, 0);
if (Input.GetKey ("s"))
transform.Translate (0, -1 * player.p_Speed * Time.deltaTime, 0);
if (Input.GetKey ("w"))
transform.Translate (0, 1 * player.p_Speed * Time.deltaTime, 0);
Debug.Log (Input.GetAxis ("Vertical"));
*/
}
}
Here's my problem. I start the game. And I'll move the ship to the left or right it doesn't really matter and everything works fine and dandy. When I decide to go back the other direction however and stop it at its starting position I notice that the ship has moved further up on the screen. I think that it is because that the rotate and translate statements don't happen at the same time but I have no clue of a workaround for this. Does anyone know of a way to fix this problem? Thanks in advance.
How is your camera following the ship? Is there a script or is the camera a child of the ship?
The camera is static in this game because its trying to simulate a 3D perspective. Google the game if you're not familiar with it. =) It's pretty awesome.
Answer by gfoot · Feb 02, 2014 at 09:43 PM
I don't fully understand the motion you're trying to create here, but I think your problem is that when you move left or right you rotate before translating, whichever way you're moving. This means they are not inverses of each other.
The inverse of "rotate then translate" is "translate backwards then rotate backwards". So for example if you swapped lines 20 and 21, you might find the up/down drift goes away.
Gyruss is set up in which the player can move in a circular pattern around the entire screen. Whatever direction you push is where your little ship will go on the circle. I'm just trying to get the whole circular pattern of movement down. I'll try your answer though and see if it works. Thanks. =)
So if you keep going left you want your ship to move in a full circle? That's also not going to work with your code how it is now, for similar reasons - it will spiral inwards.
You could use a rotation about the circle's centre point ins$$anonymous$$d of a translation, to partially fix that, but it will still drift a bit due to rounding errors.
Just tried it out on a quick run and it works like a charm! You sir have been most helpful! I have a different problem now but I think that requires a different question.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Tube Shooter Movement Design Help 1 Answer
Porting to Windows: Translate problem 2 Answers
stop rotate around c# 1 Answer