- Home /
Question by
rhianleake96 · Apr 18, 2018 at 05:54 PM ·
positiony-axisx-axis
How to stop a Gameobject moving on the X axis
Im building a game of a similar layout to 'Pong' and i need to stop the Widget on either side of the screen moving on the X-axis, and only the Y. How do I do this? (Code below)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ClickDrag : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
void Start ()
{
}
void Update ()
{
}
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
Comment
Answer by Kciwsolb · Apr 18, 2018 at 06:08 PM
You can probably just ignore the x value of the mouse position and substitute 0 instead when you create your Vector3.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161035im_/https://answers.unity.com/themes/thub/images/avi.jpg)