why does my player move when anywhere on screen is touched?
so, literally no clue what im doing. But ive been up for awhile now trying to figure out what i need to add and where to my code so my player only moves when i tap and hold on the player. Im trying to recreate galaga for android and my project is in 2D. Ive read that i need to define the players box collider so the player can only move when said collider is tapped and held. I have no clue how to do this and the threads i have read seem fairly confusing. heres the C# script i have on my player:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public GameObject character; public float speed = 500.0f;
void Start() { Input.multiTouchEnabled = false; }
void Update() { if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f)); character.transform.Translate(Vector3.MoveTowards(character.transform.position, target, speed * Time.deltaTime) - character.transform.position);
} } }
Answer by BlazeDell · Oct 15, 2018 at 04:42 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public GameObject character;
public float speed = 500.0f;
void Start()
{
Input.multiTouchEnabled = false;
}
void Update()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
character.transform.Translate(Vector3.MoveTowards(character.transform.position, target, speed * Time.deltaTime) - character.transform.position);
} } }
Your answer
Follow this Question
Related Questions
How to move a Gameobject in an Android Game with ScreenLandscape? 0 Answers
Animation frezzing on first few frames android 0 Answers
Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers
ScriptedImporter in Android build 0 Answers
How to create a movement script for my person by controlling him by joystick 0 Answers