- Home /
Tube Shooter Movement Design Help
So I'm making a 2D "Tube Shooter". If you're not sure what that is look up Gyruss. I'm having a bit of a problem with my implementation of the circular pattern of movement.
I've decided to implement the movement using a combination of rotation and translation functions. This makes the ship appear like it is moving in a circular pattern while keeping the front of the ship pointed to the center. I feel like I may be taking the hard road here in implementation of this feature of the game. Here is my code for control.
using UnityEngine;
using System.Collections;
public class PlayerControl : ShipClass
{
public Transform shotStart;
public PlayerShot shot;
void FixedUpdate ()
{
//Movement Control
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.Translate (-1 * p_Speed * Time.deltaTime, 0, 0);
transform.Rotate (0, 0, -p_rotateSpeed);
break;
default:
break;
}
//Y axis isn't implemented currently as I'm just testing out moving left and right.
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;
}
//Firing mechanism
/*
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"));
*/
}
}
My problem comes from the way the ship moves. It doesn't move in a perfect circle. More specifically when the ship moves from its starting position to the top of the screen it has shifted slightly on the X axis when I want it to end up in the same position just a direct inverse of the Y starting position.
Here are my questions. Is there a better way to implement this? If not is there something I'm missing that makes the ship act like this?
Answer by robertbu · Feb 03, 2014 at 01:23 AM
You should check into Transform.RotateAround(). If you have the camera with rotation (0,0,0) and position (0,0,x), getting Gyruss type functionality is as simple as:
using UnityEngine;
using System.Collections;
public class Control : MonoBehaviour {
public float rotateSpeed = 8.0f;
void FixedUpdate() {
transform.RotateAround(Vector3.zero, Vector3.forward, rotateSpeed * Input.GetAxis ("Horizontal"));
}
}
You could also do the rotation in Update() instead of FixedUpdate() and scale the rotation by Time.deltaTime.
Hey you know what I hadn't thought about rotating around the center object. Someone else suggested using FixedUpdate() over Update() because of the 2D collision. Is there a huge difference? Thanks you've been a huge help!
No huge difference, though it may not buy you any help with the collisions.
http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
Rotation and Translation problem 1 Answer
Distribute terrain in zones 3 Answers
Rotating while rotating? 1 Answer