unity 鼠标拖动

2020-05-20  本文已影响0人  Albert_d37d

using UnityEngine;

using System.Collections;

public class ObjectDragger : MonoBehaviour

{

private Vector3 screenPoint;

private Vector3 offset;

bool dragged = false;

private Vector3 newPosition;

private Rigidbody body;

void Awake(){

body = gameObject.GetComponent<Rigidbody>();

newPosition = transform.position;

}

void OnMouseDown()

{

    screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

}

void OnMouseDrag()

{

dragged = true;

}

void FixedUpdate(){

if (dragged){

dragged = false;

Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

newPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;

if (body != null)

body.velocity = (newPosition - transform.position) / Time.deltaTime;

}

}

void LateUpdate(){

transform.position = newPosition;

}

}

上一篇 下一篇

猜你喜欢

热点阅读