Quantcast
Channel: Questions in topic: "collisiondetection"
Viewing all articles
Browse latest Browse all 122

Player doesn't move as intended using touch controls

$
0
0
Hello i'm new to unity , i'm making a mini android game where the player is supposed to move around collect coins , and when it hits into an obstacle it's healthbar drops until it dies.Everything works perfectly fine when i test in unity but when i tried to test the game on my phone (made two scripts for the movements of the player one with keyboard keys , the other using phone touch) the player can only move up and horizontally .I need to move everywhere around the screen this is the script of the player movements using UnityEngine; using UnityEngine.EventSystems; public class f : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { Camera mainCamera; float zAxis = 0; Vector3 clickOffset = Vector3.zero; // Use this for initialization void Start() { mainCamera = Camera.main; if (mainCamera.GetComponent() == null) mainCamera.gameObject.AddComponent(); } public void OnBeginDrag(PointerEventData eventData) { zAxis = transform.position.z; clickOffset = transform.position - mainCamera.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, zAxis)) + new Vector3(0, 3, 0); transform.position = new Vector3(transform.position.x, transform.position.y, zAxis); } public void OnDrag(PointerEventData eventData) { //Use Offset To Prevent Sprite from Jumping to where the finger is Vector3 tempVec = mainCamera.ScreenToWorldPoint(eventData.position) + clickOffset; tempVec.z = zAxis; //Make sure that the z zxis never change transform.position = tempVec; } public void OnEndDrag(PointerEventData eventData) { transform.position = new Vector3(transform.position.x, transform.position.y, -1); } //Add Event System to the Camera void addEventSystem() { GameObject eventSystem = new GameObject("EventSystem"); eventSystem.AddComponent(); eventSystem.AddComponent(); } } ## ---------- ---------- if anyone knows what seems to be the problem please give me a answer thanks in advance . ## ----------

Viewing all articles
Browse latest Browse all 122

Latest Images

Trending Articles





Latest Images