You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3d中Kinematic Arrive算法报错CS0103:'LeftPoint'未定义如何解决?

修复Unity中CS0103: 'LeftPoint'不存在的错误

错误原因

你代码里直接使用了LeftPoint.point,但从未定义LeftPoint变量,C#编译器找不到这个名称对应的对象,因此抛出CS0103错误。从代码注释的描述来看,你想用鼠标点击位置作为移动目标,所以需要补充射线检测逻辑来生成合法的LeftPoint变量。

修复后的完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {
    [SerializeField] private Transform trans;
    [SerializeField] private Rigidbody rb;
    [SerializeField] private Transform target;
    [SerializeField] private Rigidbody Rigidb;

    private float maxSpeed;
    private float radiusOfSat;

    void Start () {
        maxSpeed = 5f;
        radiusOfSat = 2f;
    }
    
    void Update () {
        // 新增:通过鼠标射线检测获取地面点击点
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit LeftPoint))
        {
            Vector3 towards = LeftPoint.point - transform.position; 
            transform.LookAt(new Vector3(LeftPoint.point.x, transform.position.y, LeftPoint.point.z)); 
            if (towards.magnitude > radiusOfSat) 
            {
                towards.Normalize();
                towards *= maxSpeed;
                Rigidb.velocity = towards;
            }
            else
            {
                // 到达目标区域后停止移动
                Rigidb.velocity = Vector3.zero;
            }
        }
    }
}

关键修改说明

  • 补充射线检测逻辑:用主相机向鼠标位置发射射线,通过Physics.Raycast获取碰撞结果并存入LeftPoint(RaycastHit类型),这样LeftPoint.point就有了合法的目标坐标。
  • 完善到达行为:当物体进入目标半径内时,将刚体速度设为0,符合Kinematic Arrive算法的减速停止逻辑。
  • 注意事项:确保场景中的地面或目标物体添加了Collider组件,否则射线检测会失败,移动逻辑不会执行。

内容的提问来源于stack exchange,提问作者Tobi Ladipo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:01:51