Rotating around z-axis with OnScreen buttons
So I am working on a 2d top down space shooter game for mobile devices. So I started by making it so you can move the ship with the w,a,s,d keys. Now I got it to work but I needed it to be for mobile devices, so I made buttons and got it so the Up and Down arrow work, but the Left and Right arrow don't work. This script "PlayerMovementTouch" is attached to the players ship.
using UnityEngine;
using System.Collections;
public class PlayerMovementTouch : MonoBehaviour
{
public float Speed = 15;
public float RotSpeed = 180;
void FixedUpdate()
{
move ();
}
public void move()
{
if (GameObject.Find ("CanvasButtons").GetComponent<TouchMovement> ().UpArrow)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, Speed);
}
if (GameObject.Find ("CanvasButtons").GetComponent<TouchMovement> ().DownArrow)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, -Speed);
}
if (GameObject.Find ("CanvasButtons").GetComponent<TouchMovement> ().LeftArrow)
{
Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
z -= Input.GetAxis ("Horizontal") * RotSpeed * Time.deltaTime;
rot = Quaternion.Euler (0, 0, z);
transform.rotation = rot;
}
if (GameObject.Find ("CanvasButtons").GetComponent<TouchMovement> ().RightArrow)
{
Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
z -= Input.GetAxis ("Horizontal") * RotSpeed * Time.deltaTime;
rot = Quaternion.Euler (0, 0, z);
transform.rotation = rot;
}
}
}
Now this script "TouchMovement" is attached to a canvas where all the buttons are.
using UnityEngine;
using System.Collections;
public class TouchMovement : MonoBehaviour
{
public bool UpArrow = false;
public bool DownArrow = false;
public bool LeftArrow = false;
public bool RightArrow = false;
public void upArrow()
{
StartCoroutine("Uparrow");
Debug.Log ("UPARROW");
}
public void downArrow()
{
StartCoroutine("Downarrow");
Debug.Log ("DOWNARROW");
}
public void leftArrow()
{
StartCoroutine("Leftarrow");
Debug.Log ("LEFTARROW");
}
public void rightArrow()
{
StartCoroutine("Rightarrow");
Debug.Log ("RIGHTARROW");
}
IEnumerator Uparrow()
{
UpArrow = true;
yield return new WaitForSeconds (1);
UpArrow = false;
}
IEnumerator Downarrow()
{
DownArrow = true;
yield return new WaitForSeconds (1);
DownArrow = false;
}
IEnumerator Leftarrow()
{
LeftArrow = true;
yield return new WaitForSeconds (1);
LeftArrow = false;
}
IEnumerator Rightarrow()
{
RightArrow = true;
yield return new WaitForSeconds (1);
RightArrow = false;
}
}
So my problem is I need it when the Left arrow is pushed it moves around the Z axis by it's Rotation Speed Counter Clockwise, while the Right arrow moves Clockwise. Right now the up and down button work but not the Left and Right. Any help would be great!
Answer by Light997 · Feb 06, 2016 at 10:55 AM
It should work if you get rid of the Input.GetAxis(...) functions. These return 0 unless you are pressing a key on the keyboard (in this case A, D, RightArrow, and LeftArrow), and, as we all learned in school, multiplying something by 0 gives us 0.
Since there is no way to press keys on a touchscreen, I recommend you either:
Get rid of the function entirely
Add Defines in front of it
means:
if (GameObject.Find ("CanvasButtons").GetComponent<TouchMovement> ().LeftArrow) { Quaternion rot = transform.rotation; float z = rot.eulerAngles.z; #if UNITY_EDITOR, UNITY_STANDALONE z -= Input.GetAxis ("Horizontal") * RotSpeed * Time.deltaTime; #else z -= RotSpeed * Time.deltaTime; #endif rot = Quaternion.Euler (0, 0, z); transform.rotation = rot; }
This would allow you to still use the keys in the Editor and on Standalone builds, but the code would not be compiled for mobile. Of course, you have to do the same thing with += for the clockwise turning.
Also, get rid of your GameObject.Find and GetComponent<> functions in FixedUpdate and put them in Start and store them in a variable, otherwise, you will encounter enormous performance issues if you add more buttons, especially on mobile.
What this means is:
void Start()
{
TouchMovement CanvasButtons = GameObject.Find("CanvasButtons").GetComponent<TouchMovement>();
...
...
...
}
Then later, you can just call the variable from your stored references.
Let me know if this helped you!
@Light Thank you so much it technically does what i asked for, but some new problems. Now the first one is that it I tested it on my android and it does work, but i want it to rotate when I am holding down the button ins$$anonymous$$d of rotating for 1 second. I know that I have it set up to go for 1 second, but that is because I still have no idea how to do it for when I am holding down the button. Another problem is that when i rotate i want it to move forward based on the way the spaceship is facing. I just realized that i have it moving on the x axis ins$$anonymous$$d of the way the player is facing, which I need to find a way to fix. I hate to ask for more from you but any help would be appreciate. Thanks again!
I'l get back to you as soon as possible, unfortunately, it's quite late now in Central Europe (close to midnight) and I do not have access to a computer. Expect an answer around midday tomorrow, when I am not typing on a tablet that is quite large but still gives me a viewport 3 times smaller than that of my PC. @mateo4632
@mateo4632 Try this:
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovementTouch : $$anonymous$$onoBehaviour
{
public float Speed = 15;
public float RotSpeed = 180;
private Rigidbody2D playerRigidbody;
private Touch$$anonymous$$ovement touch$$anonymous$$ovement;
void Awake()
{
playerRigidbody = GetComponent<Rigidbody2D>();
touch$$anonymous$$ovement = GameObject.Find("CanvasButtons").GetComponent<Touch$$anonymous$$ovement>();
}
void FixedUpdate()
{
$$anonymous$$ove();
}
public void $$anonymous$$ove()
{
if (touch$$anonymous$$ovement.UpArrow)
{
playerRigidbody.AddForce(transform.up * Speed);
}
if (touch$$anonymous$$ovement.DownArrow)
{
playerRigidbody.AddForce(transform.up * -Speed);
}
if (touch$$anonymous$$ovement.LeftArrow)
{
Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
#if UNITY_EDITOR || UNITY_STANDALONE
z -= Input.GetAxis("Horizontal") * RotSpeed * Time.deltaTime;
#else
z -= RotSpeed * Time.deltaTime;
#endif
rot = Quaternion.Euler(0, 0, z);
transform.rotation = rot;
}
if (touch$$anonymous$$ovement.RightArrow)
{
Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
#if UNITY_EDITOR || UNITY_STANDALONE
z += Input.GetAxis("Horizontal") * RotSpeed * Time.deltaTime;
#else
z += RotSpeed * Time.deltaTime;
#endif
rot = Quaternion.Euler(0, 0, z);
transform.rotation = rot;
}
}
}
This should move the player dependent on their rotation with transform.up.
For the holding the button problem, look at this post:
Unity Answers - $$anonymous$$aking a hold button for the new UI
Thank you so much @Light you've helped me so much and I can properly fly around in my game on my mobile device. You saved from hours of trying to figure it out thanks again!