Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Sliding cube around on x/z axis with a constant y-axis

$
0
0
Hi, I have tried several ways of doing this but I cannot get it right. I'm basically sliding a cube on a plane. The plane is just there for visual effect. I don't want to use a rigid body, etc. I'm using the following code for the dragging: 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 dragPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); dragPosition = Camera.main.ScreenToWorldPoint(dragPosition) + offset; dragPosition.y = 2.0f; transform.position = dragPosition; } The cube needs to slide around the screen but remain of a y value of 2.0f. The cube moves on the X and Z axis but I want the y-axis to stay constant on 2.0f, but it goes out of sync with the cursor. Can anyone give me a pointer in the right direction?

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>