Trying to drag GameObject using touch input
I'm trying to combine 2 scripts to get my player to follow the movement of my finger across the screen. I was trying to combine this awesome YouTube tutorial that I like for movement with the 'MOBILE DEVELOPMENT: CONVERTING SPACE SHOOTER TO MOBILE' Unity tutorial video for creating a touch pad and making my player follow my finger. But my PlayerController script is different and I'm having trouble combining the two. So far, I can get the Debug.Log to provide a readout of where my finger is, but for some reason, I can't get the ship to actually follow the same position as my finger's current position in screen space(I'm pretty much stuck at the 52 minute point in the video, where the original movement script is readjusted for touch input based on what's in the SimpleTouchPad script. Can anyone help me?
Thanks in advance, Millie
Here's Unity's script:
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections;
public class SimpleTouchPad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler {
private Vector2 origin;
private Vector2 location;
void Awake()
{
location = Vector2.zero;
}
public void OnPointerDown(PointerEventData data)
{
origin = data.position;
}
public void OnDrag(PointerEventData data)
{
Vector2 currentPosition = data.position;
Vector2 directionRaw = currentPosition - origin;
Vector2 location = directionRaw.normalized;
Debug.Log(location);
}
public void OnPointerUp(PointerEventData data)
{
location = Vector2.zero;
}
public Vector2 GetDirection()
{
return location;
}
}
And here's my script (trying to implement the change required):
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerControl : MonoBehaviour {
public GameObject GameManagerGO;
public GameObject PlayerBullet1GO;
public GameObject bulletPosition01;
public GameObject bulletPosition02;
public GameObject ExplosionGO; //explosion prefab
public float fireRate = 0.5f;
public float speed = 3f;
public SimpleTouchPad touchPad;
// Use this for initialization
void Start()
{
InvokeRepeating("Shoot", 2f, fireRate);
}
// Update is called once per frame
void Update()
{
//web version
/*
float x = Input.GetAxisRaw("Horizontal"); //-1 = left, 1 = right
float y = Input.GetAxisRaw("Vertical"); //-1 = down, 1 = up
*/
//compute a direction vector based on the input and normalize it to get a unit vector
Vector2 direction = new Vector2(x, y).normalized;
//Trying to use touchPad from SimpleTouchPad script to influence movement, but it won't work
Vector2 location = touchPad.GetDirection();
//This was the part in the tutorial that was readjusted to reflect the Vector2 value set in
//SimpleTouchPad
Vector2 movement = new Vector2(location.x, location.y);
Move(direction);
}
void Move(Vector2 direction)
{
//Find the screen limits to player's movement(left, right, top and bottom edge
//of the screen)
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); //bootom-left corner)
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); //top-right corner;
max.x = max.x - 0.225f; //subtract the player Sprite half width
min.x = min.x + 0.225f; //add player sprite half width;
max.y = max.y - 0.285f; //subtract player sprite half height
min.y = min.y + 0.285f; //add player sprite half height
//Get player's current position
Vector2 pos = transform.position;
//Calculate the new position
pos += direction * speed * Time.deltaTime;
//Make sure the new position is not outside the screen
pos.x = Mathf.Clamp(pos.x, min.x, max.x);
pos.y = Mathf.Clamp(pos.y, min.y, max.y);
//Update the player's position
transform.position = pos;
}
//Function to make player shoot
public void Shoot()
{
//play the laser sound effect
// GetComponent<AudioSource>().Play();
//Instantiate the first bullet
GameObject bullet01 = (GameObject)Instantiate(PlayerBullet1GO);
bullet01.transform.position = bulletPosition01.transform.position; //set bullet's initial position
//Instantiate the second bullet
GameObject bullet02 = (GameObject)Instantiate(PlayerBullet1GO);
bullet02.transform.position = bulletPosition02.transform.position; //set bullet's initial position
}
}