Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseReturns the RaycastResult associated with a mouse click, gamepad button press or screen touch.
PointerEventData.pointerPressRaycast returns the RaycastResult hit result when the game requests the player press details.
The example below uses the IBeginDragHandler
, IDragHandler
, and IEndDragHandler
interfaces. They provide OnBeginDrag
, OnDrag
, and OnEndDrag
event handlers respectively. OnBeginDrag
queries when a raycast hits a point in World space. OnBeginDrag
sends a message to PointerEventData.pointerPressRaycast.worldPosition
. The code example then reports the screen position when OnBeginDrag
starts.
using UnityEngine; using System.Collections; using UnityEngine.EventSystems;
// PointerEventData.pointerPressRaycast example
// A cube which can be moved by the mouse or track bar at runtime. // Up/down and left/right are supported. // // Create a 3D project and add a Cube. Position the Cube at the origin. Add a // PhysicsRaycaster component to the Main Camera. Also, add an Empty GameObject // to the Hierarchy. Apply an EventSystem component and a StandaloneInputModule // component to this Empty GameObject. Next create this script and assign it to // the Cube. Now, run the Game. The Cube can be moved up/down and left/right.
public class Example : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { private Vector3 newPosition; private Vector3 delta; private Vector3 startPosition;
void Awake() { // Move the Cube away from the origin. startPosition = new Vector3(0.25f, -0.5f, 3.0f); transform.position = startPosition;
newPosition = Vector3.zero; delta = Vector2.zero;
// Position the camera and switch to solid color. Camera.main.transform.position = new Vector3(0.5f, 1.0f, -3.0f); Camera.main.transform.localEulerAngles = new Vector3(15.0f, -20.0f, 0.0f); Camera.main.clearFlags = CameraClearFlags.SolidColor; }
public void OnBeginDrag(PointerEventData eventData) { Debug.Log("OnBeginDrag: " + eventData.pointerPressRaycast.screenPosition);
// Obtain the position of the hit GameObject. delta = eventData.pointerPressRaycast.worldPosition; delta = delta - transform.position; }
public void OnDrag(PointerEventData eventData) { newPosition = eventData.pointerCurrentRaycast.worldPosition - delta;
if (eventData.pointerCurrentRaycast.worldPosition.x == 0.0f || eventData.pointerCurrentRaycast.worldPosition.y == 0.0f) { newPosition = eventData.delta;
transform.Translate(newPosition * Time.deltaTime); return; }
transform.position = newPosition; }
public void OnEndDrag(PointerEventData eventData) { Debug.Log("OnEndDrag");
transform.position = startPosition; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!