Help with mobile touch dual stick controls. Rotate or Snap character to the right stick. (Top Down)

Hi all,

This might be a noob-of-the-year worthy question, so sorry, but I have wasted so much time already on this… Basically I found a script which I managed to implement into my game. It allows me to control my character by using 1 stick mobile input.

Sadly it makes the game rubbish as the game was originally dual stick (mouse and keys). I would really appreciate some help separating the bit of code I have into two sticks…

I have fiddled with it and searched all over and have only broken what works and have not achieved what I want so I am desperate now (I broke it so that the player turns right when I press up- but i think i will find a way to fix that myself).

Thank you for any input everyone (clearly I am not a programmer, but I am learning, just very slowly- always used PlayMaker in the past).

I would like the character to snap to face a direction (or rotate) according to the right stick, much like in geometry wars, and continue to move using the left stick.

Bit of Code I think I need to Split : (starts on line 106 in full script)

Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Horizontal"), 0f, CrossPlatformInputManager.GetAxisRaw("Vertical"));
		 if(turnDir != Vector3.zero){
			Vector3 playerToMouse = (transform.position + turnDir) - transform.position;
			playerToMouse.y = 0;
			Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
			playerRigidBody.MoveRotation(NewRotation); 
		}

Entire Script:

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.UI;

public class GrunkBehaviorScript : MonoBehaviour {

	public float speed = 2.0f;
	public GameObject gameOverPanel;
	PlayerHealth playerHealth;
	public int score;
	public Text scoreText;

	Vector3 movement;
	Rigidbody playerRigidBody;
	bool isMoving = false;
	Animator anim;
	int floorMask;
	float camRayLength = 100.0f;
	public bool isEnabled = true;
	public bool gameOver = false;

	// Use this for initialization
	void Start () {
		gameOverPanel.SetActive (false);
		GameObject player = GameObject.FindGameObjectWithTag ("Player");
		playerHealth = player.GetComponent<PlayerHealth> ();
		score = 0;
		scoreText.text = "Score: 0";
	
	}
	
	// Update is called once per frame
	void Update () {
		if (playerHealth.currentHealth <= 0) {
			isEnabled = false;
		if (!gameOver) {
			Invoke ("DisplayGameOver", 1.0f);
			}
		}

		scoreText.text = "Score: " + score.ToString ();

	}

	void Awake() {
		playerRigidBody = GetComponent<Rigidbody> ();
		anim = GetComponent<Animator> ();
		floorMask = LayerMask.GetMask ("Floor");
	}

	void FixedUpdate (){
		if (isEnabled == false)
			return;
		// get button pressed by player
		float h = CrossPlatformInputManager.GetAxisRaw ("Horizontal");
		float v = CrossPlatformInputManager.GetAxisRaw ("Vertical");

		Move (h, v);

		if (h !=0 || v != 0) {
			isMoving = true;
		} else {
			isMoving = false;
		}
		Animating ();
		Turning ();
	}


	void Move( float h, float v){
		movement.Set (v, 0f, -h);

		movement = movement.normalized * Time.fixedDeltaTime * speed;

		playerRigidBody.MovePosition (transform.position + movement);

	}


	void Animating(){
		// if char is moving, then play walk anim, if not play idle anim
		if (isMoving == true) {
			//if true play animation
			anim.SetFloat ("speed", 1);
		} else {
			anim.SetFloat ("speed", 0);
		}
	}

	void Turning(){
		//Find Mouse Position and check if in range
		//if in range, rotate character towards mouse

		#if !MOBILE_INPUT
		Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit floorHit;
		if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
			Vector3 playerToMouse = floorHit.point - transform.position;
			playerToMouse.y = 0.0f;
			Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
			playerRigidBody.MoveRotation(newRotation);

		}
		#else
		Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Horizontal"), 0f, CrossPlatformInputManager.GetAxisRaw("Vertical"));
		 if(turnDir != Vector3.zero){
			Vector3 playerToMouse = (transform.poition + turnDir) - transform.position;
			playerToMouse.y = 0;
			Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
			playerRigidBody.MoveRotation(NewRotation); 
		}
		#endif
	}


	public void DisableMovement(){
		isEnabled = false;
	}

	public void RestartGame(){
		Application.LoadLevel ("SceneWithSpidersAndStuff");
	}

	void DisplayGameOver(){
			gameOver = true;
		gameOverPanel.SetActive (true);
	}
}

I think this bit of code works perfectly with the mouse to do what I want, but im not sure:

         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit floorHit;
         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
             Vector3 playerToMouse = floorHit.point - transform.position;
             playerToMouse.y = 0.0f;
             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
             playerRigidBody.MoveRotation(newRotation);

Thanks again to anyone reading.

Well I figured it out,

The short of it is:

I added another single joystick and called the axis’ mouse x’ and ‘mouse y’ then in the first bit of script I posted I changed the’ horizontal’ axis to ‘mouse x’ and ‘vertical’ to ‘mouse y’.

Also I modified the script of the unity standard asset joystick. (Yeah, I know, me modifying scripts- im like a genius overnight). I changed a ‘+’ to a ‘-’ somewhere and now my character moves left and right correctly - but the funniest thing happened: I move my finger to right- character moves to right - but joystick sprite moves to left lol. I guess i better keep studying the script until I find where I change another ‘+’ to a ‘-’,

So essentially I think my problem is solved, here is my game running as I wish (except for the right stick flying left when I move it right lol): - YouTube

can you post the modified version of the script?