我想將對象移動到鼠標位置。但是它給了我很大的x值,比如300,但是在那個位置,預先放置的對象的x位置是-4。獲得鼠標位置統一
rigidBody.velocity = new Vector3(Input.mousePosition.x, EndPointY, 0)*4;
那麼我怎樣才能獲得當前的鼠標位置呢?
謝謝..
我想將對象移動到鼠標位置。但是它給了我很大的x值,比如300,但是在那個位置,預先放置的對象的x位置是-4。獲得鼠標位置統一
rigidBody.velocity = new Vector3(Input.mousePosition.x, EndPointY, 0)*4;
那麼我怎樣才能獲得當前的鼠標位置呢?
謝謝..
這是當前的鼠標位置。問題在於你的對象處於世界座標,鼠標正在使用屏幕座標。
您需要使用Camera.ScreenToWorldPoint()轉換鼠標位置。
Input.mousePosition會給你鼠標在屏幕上的位置(像素)。您需要使用Camera.ScreenToWorldPoint()將這些像素轉換爲世界單位。
您可以按照this link瞭解如何用鼠標拖動3d物體,或者您可以複製此代碼以將物體從當前位置移動到鼠標位置。
//the object to move
public Transform objectToMove;
void Update()
{
Vector3 mouse = Input.mousePosition;
Ray castPoint = Camera.main.ScreenPointToRay(mouse);
RaycastHit hit;
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
{
objectToMove.transform.position = hit.point;
}
}