Hey guys, I apologize if this has been said before, but I’m having an issue with the movement I want to achieve in a game I’m making. In its simplest form, I want to just be able to drag a box across the screen (touch controls). However, I have no idea how to lock the current boxes Z-axis to it stays the same. I tried setting it to its local Z value using transform.position.z, no luck. I tried setting it to just 0 so I could at least see the script, but it keeps becoming -10 for some reason when I touch the screen. Strangely enough, I only have this problem when the camera is in perspective view.
My question is: how can I take the boxes current Z position and keep it the same when I put my finger on the screen. I’m not sure if there’s something I’m not understanding between local and world coordinates, or what, but I’m at a loss. Code is below:
using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.touchCount == 1) //Exactly 1 finger.
{
Touch touch = Input.GetTouch(0);
//Replace these numbers with actual screen dimensions?
float x = touch.position.x;
float y = touch.position.y;
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 0f));
}
}
}