【发布时间】:2015-08-01 04:43:27
【问题描述】:
我正在尝试为罚球球游戏制作 C# 脚本,需要获取鼠标在世界中的位置,并且它应该是 3D 的,以便能够在所有 3 个轴上进行投掷。我对脚本编写有点陌生,我编写的脚本有效,但不正确。我不确定如何让深度或 y 轴工作,因为屏幕只有 2d。
using UnityEngine;
using System.Collections;
public class ShootBall : MonoBehaviour {
private Rigidbody rb;
private RaycastHit hit;
private Vector3 com;
private Vector3 shootDirection;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update () {
}
void OnMouseDown (){
com = rb.worldCenterOfMass;
Debug.Log (com);
}
void OnMouseDrag (){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log(ray);
Physics.Raycast(ray, out hit);
}
void OnMouseUp (){
shootDirection = com - hit.point;
rb.AddForce (shootDirection * 100);
}
}
【问题讨论】:
标签: c# optimization unity3d mouse