- Home /
"Working with moving and mouse touch to set coordinates"
I'm currently trying to make a 2D game for mobile that works like this:
there are 4 squares in the middle of the screen, the player starts inside one of the squares's middle, the player can only move on the X and Y axis, once the player touches a square(that is not diagonal to the player) the player has to move to it and is not allowed to change direction until he gets inside of the middle.
This was the code I was using, the problem is that the player's character goes to wherever the mouse is in the squares, not on touch, so even if i put on diagonal he goes there. Not to mention he can change direction even if he is not in the middle of the square
TR=top right square Tl=top Left square BR=bottom Right Bl=Bottom left
INTL=inside tl INTR=inside tr INBL=inside bl INBR=inside br
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moving : MonoBehaviour {
public float Speed = 5.0f;
public bool INTR = true;
public bool INTL =true;
public bool INBR =true;
public bool INBL =true;
public bool TR = false;
public bool TL =false;
public bool BL =false;
public bool BR =false;
public int timerTR=0;
public int timerTL=0;
public int timerBR=0;
public int timerBL=0;
void Start () {
}
// Update is called once per frame
void Update () {
CastRay ();
MoveTowardsTarget ();
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Pressed left click, casting ray.");
}
}
private void MoveTowardsTarget ()
{
}
void CastRay() {
float speed = 1;
Vector3 targetPosition = new Vector3 (-1.5f, 0, 0);
Vector3 targetPosition2 = new Vector3 (1.5f, 0, 0);
Vector3 targetPositiony = new Vector3 (1.5f, -4.5f, 0);
Vector3 targetPositiony2 = new Vector3 (-1.5f, -4.5f, 0);
Vector3 currentPosition = this.transform.position;
if (Vector3.Distance (currentPosition, targetPositiony2) > .1f && INBL == false && BL == true && TR == false && INTR == false) {
Vector3 directionOfTravel = targetPositiony2 - currentPosition;
directionOfTravel.Normalize ();
this.transform.Translate (
(directionOfTravel.x * Speed * Time.deltaTime),
(directionOfTravel.y * Speed * Time.deltaTime),
(directionOfTravel.z * Speed * Time.deltaTime),
Space.World);
INBL = true;
INTL = false;
INTR = false;
INBR = false;
}
if (Vector3.Distance (currentPosition, targetPositiony) > .1f && BR == true && INBR == false && TL == false && INTL == false) {
Vector3 directionOfTravel = targetPositiony - currentPosition;
directionOfTravel.Normalize ();
this.transform.Translate (
(directionOfTravel.x * Speed * Time.deltaTime),
(directionOfTravel.y * Speed * Time.deltaTime),
(directionOfTravel.z * Speed * Time.deltaTime),
Space.World);
INBR = true;
INBL = false;
INTL = false;
INTR = false;
}
if (Vector3.Distance (currentPosition, targetPosition) > .1f && TL == true && INTL == false && BR == false && INBR == false) {
Vector3 directionOfTravel = targetPosition - currentPosition;
directionOfTravel.Normalize ();
this.transform.Translate (
(directionOfTravel.x * Speed * Time.deltaTime),
(directionOfTravel.y * Speed * Time.deltaTime),
(directionOfTravel.z * Speed * Time.deltaTime),
Space.World);
INTL = true;
INTR = false;
INBL = false;
INBR = false;
}
if (Vector3.Distance (currentPosition, targetPosition2) > .1f && TR == true && INTR == false && BL == false && INBL == false) {
Vector3 directionOfTravel = targetPosition2 - currentPosition;
directionOfTravel.Normalize ();
this.transform.Translate (
(directionOfTravel.x * Speed * Time.deltaTime),
(directionOfTravel.y * Speed * Time.deltaTime),
(directionOfTravel.z * Speed * Time.deltaTime),
Space.World);
INTR = true;
INBL = false;
INBR = false;
INTL = false;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
if (hit) {
Debug.Log (hit.collider.gameObject.name);
}
if (hit.collider.gameObject.name ==("TL")) {
Debug.Log ("indo para tl");
TR = false;
TL=true;
BL=false;
BR=false;
INTL = false;
}
if (hit.collider.gameObject.name ==("TR") ) {
TR = true;
TL=false;
BL=false;
BR=false;
INTR = false;
Debug.Log ("indo para tr");
}
if (hit.collider.gameObject.name ==("BL") ) {
TR = false;
TL=false;
BL=true;
BR=false;
INBL = false;
Debug.Log ("indo para bl");
} if (hit.collider.gameObject.name ==("BR")) {
TR = false;
TL=false;
BL=false;
BR=true;
INBR = false;
Debug.Log ("indo para br");
}
}
}
what am i doing wrong?