- Home /
Question by
mika132132 · Jun 18, 2018 at 05:34 PM ·
movementbuttonobjectcoordinatesarrow
Moving object with x,y,z coordinate with arrows around object
Hello!
I have tried to make system where i can move object with x,y,z arrows. Same system how i can move objects in Unity editor. Here is image how that looks now. There is missing z axel in this point. Object moves like it should but when i rotate camera X button will rotate with camera and then it is totally wrong place.
Here is code:
void OnGUI() {
Vector3 pointWhere2 = camera.WorldToScreenPoint(gameObject.transform.position);
buttonRect = new Rect(pointWhere2.x, Screen.height - pointWhere2.y - 90, 20, 50);
if(buttonRect.Contains(Event.current.mousePosition))
{
if(Event.current.type == EventType.MouseDown)
{
buttonPressed = true;
}
if(Event.current.type == EventType.MouseUp)
{
buttonPressed = false;
}
}
if(buttonPressed && Event.current.type == EventType.MouseDrag)
{
Vector3 mousePos = camera.WorldToScreenPoint(Input.mousePosition);
float xPos = gameObject.transform.position.x;
float yPos = gameObject.transform.position.y;
float zPos = gameObject.transform.position.z;
xPos = gameObject.transform.position.x;
yPos -= Event.current.delta.y;
zPos = gameObject.transform.position.z;
Vector3 mouse = Input.mousePosition;
gameObject.transform.position = new Vector3(xPos, yPos, zPos);
}
GUI.Button(buttonRect, "Y", levelData.arrowY);
buttonRect = new Rect(pointWhere2.x+90, Screen.height - pointWhere2.y, 50, 20);
if(buttonRect.Contains(Event.current.mousePosition))
{
if(Event.current.type == EventType.MouseDown)
{
buttonPressed2 = true;
}
if(Event.current.type == EventType.MouseUp)
{
buttonPressed2 = false;
}
}
if(buttonPressed2 && Event.current.type == EventType.MouseDrag)
{
Vector3 mousePos = camera.WorldToScreenPoint(Input.mousePosition);
float xPos = gameObject.transform.position.x;
float yPos = gameObject.transform.position.y;
float zPos = gameObject.transform.position.z;
xPos -= Event.current.delta.x;
yPos = gameObject.transform.position.y;
zPos = gameObject.transform.position.z;
Vector3 mouse = Input.mousePosition;
gameObject.transform.position = new Vector3(xPos, yPos, zPos);
}
GUI.Button(buttonRect, "X", levelData.arrowX);
}
screenshot-6.png
(5.1 kB)
Comment