Unity点击物体后,移动到物体所在位置

Unity点击物体后,移动到物体所在位置

方法一:OnMouse检测(需要Collider组件)

脚本挂在被点击的物体上

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

/// <summary>
/// 缺点:要挂在所有需要被检测的物体上
/// </summary>
public class Move01 : MonoBehaviour
{
    public GameObject Player; //可以直接通过手动拖拽找到物体,也可以通过名字或tag查找

    private void Start()
    {
        //Player = GameObject.Find("Player"); //通过名字查找物体
        //Player = GameObject.FindWithTag("cube"); //通过tag查找物体
    }

    void OnMouseDown()
    {
        Player.transform.localPosition = this.transform.localPosition; //position是世界位置,localPosition是相对父节点的位置;如果物体没有父节点,localpositon和position没有区别;如果物体有父节点,用position
        Debug.Log("已移动");
    }
}

方法二: 射线检测(需要Collider组件)

脚本挂在角色控制器上

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

/// <summary>
/// 缺点:当场景中有大量物体时,会检测最近的物体,导致无法获取真正想要得到的物体
/// </summary>
public class Move02 : MonoBehaviour
{
    Ray ray;
    RaycastHit hit;
    GameObject obj;

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                obj = hit.collider.gameObject;
                string name = obj.name;
                string tag = obj.tag;
                name = name.Trim(); //去掉名字前后的空格符
                tag = tag.Trim(); //去掉tag前后的空格符
                
                //通过名字判断是否是想要的路标物体
                if (obj.name == "Cube")
                {
                    this.transform.localPosition = obj.transform.localPosition;
                }
                //通过tag判断是否是想要的路标物体
                if (obj.tag.Equals("cube"))
                {
                    this.transform.localPosition = obj.transform.localPosition;
                }
            }
        }
    }
}

改进:使用Raycast中的LayerMask

  1. 创建“cube”层
  2. 将想要被检测的物体放入该层
    layer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 使用Raycast中的LayerMask,处理多物体场景中的特定物体检测
/// </summary>
public class move : MonoBehaviour
{
    Ray ray;
    RaycastHit hit;
    GameObject obj;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("检测到点击");
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100, LayerMask.GetMask("cube")))
            {
                obj = hit.collider.gameObject;
                this.transform.position =obj.transform.position;
            }
        }
    }
}

方法三:EventTrigger 物体动态事件监听

3.1、 3D物体事件监听

  1. 在相机上挂Physics Raycaster组件
    Physics Raycaster组件
  2. 检查是否有EventSystem
    EventSystem
  3. 将脚本挂在被点击的物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 缺点:要挂在所有需要被检测的物体上
/// </summary>
public class mouseDown : MonoBehaviour
{
    private GameObject Player;
    private void Start()
    {
        Player = GameObject.Find("Player");
    }

    public void MyClick()
    {
        Player.transform.position = this.transform.position;
    }
}

  1. 添加组件Event Trigger组件
    Event Trigger组件

3.2、 世界UI世界监听

  • 0
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值