C# 让玩家跟随鼠标光标移动(无需单击/拖动)

C# 让玩家跟随鼠标光标移动(无需单击/拖动),c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我试图让一个玩家移动到我的鼠标光标位置。我正在尝试找出光线投射,并已成功调试鼠标位置的全局坐标。。然而,我很难弄明白为什么我的球员会朝y方向射击。。起初,我认为这取决于我的相机位置,因为当我尝试这之前,坐标是静态的。所以我特别设置了一个参考我的球员位置,看看是否是这样,这就是问题所在,但仍然会飞上天空。。我试过用一个刚体来增加力,它有点起作用,但不管出于什么原因,它只在y轴旋转上起作用。我尝试了transform.position来测试代码,虽然有效,但仍然存在向Y方向发射的问题,并且在tran

我试图让一个玩家移动到我的鼠标光标位置。我正在尝试找出光线投射,并已成功调试鼠标位置的全局坐标。。然而,我很难弄明白为什么我的球员会朝y方向射击。。起初,我认为这取决于我的相机位置,因为当我尝试这之前,坐标是静态的。所以我特别设置了一个参考我的球员位置,看看是否是这样,这就是问题所在,但仍然会飞上天空。。我试过用一个刚体来增加力,它有点起作用,但不管出于什么原因,它只在y轴旋转上起作用。我尝试了transform.position来测试代码,虽然有效,但仍然存在向Y方向发射的问题,并且在transform.translate中使用了Vector3.movetoward和Vector3.Lerp。。。所有问题都是一样的,我点击play,当我的raycast点击时,游戏对象就会射向空中,直到我出现控制台错误,说太远了

public class MousePlayerController : MonoBehaviour
{

    public float speed = 10f; 
    private Camera cam;
    private Rigidbody playerRb;
    private Vector3 playerPos;
    private float heightLimit = 0.5f;
    // Start is called before the first frame update
    void Start()
    {
        cam = Camera.main;
        playerRb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        playerPos = GameObject.Find("Player").transform.position;
        Ray mousePos = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit rayLength;

        if(Physics.Raycast(mousePos, out rayLength, Mathf.Infinity)) {
            Debug.Log(rayLength.point);
            Debug.DrawLine(mousePos.origin, rayLength.point, Color.blue);
            transform.Translate(Vector3.Lerp(playerPos, rayLength.point, speed * Time.deltaTime));
            //transform.position = Vector3.Lerp(playerPos, rayLength.point, speed * Time.deltaTime);
            //playerRb.AddForce(rayLength.point * speed * Time.deltaTime);
            if (transform.position.y > 1f) {
                //playerPos = new Vector3(transform.position.x, heightLimit, transform.position.z);
            }
        }
    }
}
公共类MousePlayerController:MonoBehavior
{
公共浮子速度=10f;
私人摄像机;
私人刚体运动员b;
私人矢量3播放器;
专用浮子高度限值=0.5f;
//在第一帧更新之前调用Start
void Start()
{
cam=Camera.main;
playerRb=GetComponent();
}
//每帧调用一次更新
无效更新()
{
playerPos=GameObject.Find(“Player”).transform.position;
Ray mousePos=cam.ScreenPointRoay(输入.mousePosition);
光线长度;
if(Physics.Raycast(鼠标点、输出光线长度、数学无穷大)){
Debug.Log(光线长度点);
DrawLine(mousePos.origin、raylegth.point、Color.blue);
transform.Translate(Vector3.Lerp(playerPos,raydlength.point,speed*Time.deltaTime));
//transform.position=Vector3.Lerp(playerPos,raylegth.point,speed*Time.deltaTime);
//playerRb.AddForce(光线长度.点*速度*时间.增量时间);
if(变换位置y>1f){
//playerPos=新矢量3(transform.position.x、heightLimit、transform.position.z);
}
}
}
}

在这方面的任何帮助都将不胜感激,因为我正在慢慢失去处理这一问题和团结的所有希望。谢谢

So
cam.screenpointoray(Input.mousePosition)
在3d空间中给出了一个点,但是我们只关心2d坐标,所以我们可以简单地丢弃Y分量

// get mouse position
Vector3 mousePos = Camera.ScreenToWorldPoint(Input.mousePosition);

// discard y
mousePos = new Vector3(mousePos.x, 0, mousePos.z);

// we can then use this
transform.Translate(Vector3.Lerp(playerPos, mousePos, speed * Time.deltaTime));
希望这能解决你的问题