Version: 2018.3 (switch to 2019.1)
LanguageEnglish
  • C#

PointerEventData.pointerPressRaycast

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

public EventSystems.RaycastResult pointerPressRaycast;

Description

Returns 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: