Need help with this code :)

So i have this code. This code lets me to move objects.And one thing that i can’t understand is how to change it that i only could move it horizontally? Any help would be nice :).

using UnityEngine;
using System.Collections;

public class TouchControl : MonoBehaviour {

float distance = 10;

void OnMouseDrag()
{
Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);

transform.position = objPosition;
}
}

Here’s what you’ve to do:

  • Read this (especially “the do’s 5”)
  • Then this

It’s pretty easy to move it just horizontally btw, just remove the Input.mousePosition.y from the Vector3 constructor and replace it with 0.

2 Likes

Sorry :smile: And Thanks. :slight_smile: