Object turns towards a direction of button press and shoots

Hi guys, having a bit of difficulty with this one, I am new to programming and unity ect…

Basically, what I want is a space ship in the middle of the screen (With 2D editor and sprites) with 6 buttons around the object.

If you click on the top right button, the ship faces that way and shoots in that direction, if you click the bottom button, it faces that way and shoots.

We don’t have to worry about the shooting part, that should be easy, but I am having difficulty linking the buttons with the object and making it face in the direction of the button that is being clicked.

All I have currently is a small script that if you press A, it turns to the leftish, if you press W, it faces upwards, but this is really not ideal haha also I can’t get S to work. This is what I have :slight_smile: which is not what I want haha, any help would be appreciated :slight_smile: I hope I am making sense

    using UnityEngine;
    using System.Collections;
    
    public class Move : MonoBehaviour {
    
    
    	// Use this for initialization
    	void Start () {
    		
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	
    
    
    				 if (Input.GetKeyDown (KeyCode.D)) {
    						transform.right = new Vector3 (-1, -1, 0);
    				}
    			else if (Input.GetKeyDown (KeyCode.W)) {
    			transform.right = new Vector3 (1, 0, 0);
    		}
    
    		else if (Input.GetKeyDown (KeyCode.S)) {
    			transform.right = new Vector3 (1, 0, 0);   //Can't get this to work????
    		}
    	
    	
    		else if (Input.GetKeyDown (KeyCode.Q)) {
    			transform.right = new Vector3 (1, 1, 0);
    		}
    		else if (Input.GetKeyDown (KeyCode.E)) {
    			transform.right = new Vector3 (1, -1, 0);
    		}
    		else if (Input.GetKeyDown (KeyCode.A)) {
    			transform.right = new Vector3 (-1, 1, 0);
    		}
    
    	}
    }

First, your assigning vectors to transform.right is problematic. It will always work, but it may flip your object in the process. For 2D I recommend using Quaternion.AngleAxis().

So thinking about your problem, I’ve come up with a bit of simplification (since you are using world objects for buttons). What you do is tag all of your button object with the same tag. In the example code it is ‘RotateButton’. Then any time you click on one of these buttons, the sprite will be rotated to point at that button. Doing it this way simplifies the task of mapping buttons to positions…you don’t have to calculate the angle nor name the buttons in some way to communicate the angle. The position of the button becomes the angle to shoot. Note the code below assumes that you’ve constructed your sprite so that the ‘forward’ of the object is on the right (which you’ve likely done given that you are using ‘transform.right’ in your code above).

using UnityEngine;
using System.Collections;

public class PointAndFire : MonoBehaviour {
	
	void Update()
	{
		if (Input.GetMouseButtonDown (0)) {
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if (Physics.Raycast(ray, out hit) && hit.collider.tag == "RotateButton") {
				Vector3 dir = hit.transform.position - transform.position;
				float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
				transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
				// Insert call to projectile firing code here
			}
		}
	}
}