让物体移动到鼠标点击的位置 AI导航系统

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

public class Controll : MonoBehaviour
{
    // 朝向目标点移动
    public GameObject MoveTarget;
    public GameObject Player;
    Vector3 Movedir;
    public float speed;

    NavMeshAgent navMeshAgent;
    void Start()
    {
        navMeshAgent = Player.GetComponent<NavMeshAgent>();
    }
    void Update()
    {


        if (Input.GetMouseButtonDown(0))
        {
            Ray OneClickPointRay = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机出发向鼠标点击的位置
            RaycastHit OutPoint;
            if (Physics.Raycast(OneClickPointRay, out OutPoint))//如果投射射线撞击到一个物体成功
            {
                navMeshAgent.SetDestination(OutPoint.point);
            }
        }
    }
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我没有能力进行代码演示。但是,我可以告诉你一些关于C#鼠标钩子的基本知识。 C#中可以使用System.Windows.Forms命名空间下的MouseHook类来实现鼠标钩子。使用鼠标钩子可以监视并拦截鼠标消息,从而实现对鼠标的控制。 以下是一个简单的示例代码,可以使用鼠标钩子控制鼠标移动点击: ``` using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace MouseHookDemo { public partial class Form1 : Form { private const int WH_MOUSE_LL = 14; private const int WM_LBUTTONDOWN = 0x0201; private const int WM_LBUTTONUP = 0x0202; private static IntPtr hookId = IntPtr.Zero; private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); private static HookProc hookCallback = HookCallback; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [StructLayout(LayoutKind.Sequential)] private struct POINT { public int X; public int Y; } [StructLayout(LayoutKind.Sequential)] private struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; } public Form1() { InitializeComponent(); hookId = SetHook(hookCallback); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { UnhookWindowsHookEx(hookId); } private static IntPtr SetHook(HookProc hookProc) { using (ProcessModule module = Process.GetCurrentProcess().MainModule) { return SetWindowsHookEx(WH_MOUSE_LL, hookProc, GetModuleHandle(module.ModuleName), 0); } } private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN) { // 鼠标左键按下时 int x = Cursor.Position.X + 10; int y = Cursor.Position.Y + 10; Cursor.Position = new Point(x, y); } else if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONUP) { // 鼠标左键抬起时 int x = Cursor.Position.X - 10; int y = Cursor.Position.Y - 10; Cursor.Position = new Point(x, y); } return CallNextHookEx(hookId, nCode, wParam, lParam); } private static IntPtr GetModuleHandle(string lpModuleName) { IntPtr hModule = IntPtr.Zero; ProcessModuleCollection modules = Process.GetCurrentProcess().Modules; foreach (ProcessModule module in modules) { if (module.ModuleName == lpModuleName) { hModule = module.BaseAddress; break; } } return hModule; } } } ``` 在这个示例代码中,我们使用了鼠标左键按下和抬起的事件来控制鼠标移动。当鼠标左键按下时,鼠标会向右下方移动10个像素,当鼠标左键抬起时,鼠标会向左上方移动10个像素。 需要注意的是,使用鼠标钩子需要在应用程序关闭时释放钩子,否则可能会导致系统崩溃。在这个示例代码中,我们在窗体关闭事件中释放了钩子。 希望这些信息能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值