unity射线检测实现用鼠标拖拽来做刷牙模板

先制作一个Arm空物体,在Arm下创建子物体,body(一个square)和一个top空物体(将位置拖动到手臂的最顶端) 

然后再创建一个Brush空物体,将位置拖动到,再创建一个body的square

 将左上方设置为pivot,再将body拖到Brush下面,让Brush的中心点不变(保持在末端)

 建立一个Brush脚本,用于将牙刷的右端始终和手臂末端连接在一起

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

public class Brush : MonoBehaviour
{
    public Transform AnchorPoint;
    private void Update()
    {
        transform.position = AnchorPoint.position;
    }
}

 

 再建立一个空物体MouseManager,将一个新建的脚本MouseManager拖拽上去(记得将arm里的body添加collider2d,并将其tag设置为Arm)(要注意调整collider)

 

 

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

public class MouseManager : MonoBehaviour
{
    public static Action<Vector3> MouseMove;
    public GameObject Arm;
    //public Arm armScript;
    public Transform MyPosition;
    public float maxAngle;

    private bool bInRange;
    private bool bClick;
    private Vector3 currentPosition;
    private float currentAngle;//鼠标与y轴的角度

 
    private void Update()
    {
        bInRange = false;
        if (Input.GetMouseButtonDown(0))
        {
            bClick = true;
        }
        if (Input.GetMouseButtonUp(0))
        {
            bClick  = false;
        }
        var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        var info = Physics2D.Raycast(pos, pos - transform.position);
        if(info.collider != null)
        {
            //Debug.Log("collide sth");
            if (info.collider.gameObject.CompareTag("Arm"))
            {
                Debug.Log("arm!!!!");
                bInRange = true;
            }

        }

        RotateArm();
        



    }


    public void RotateArm()
    {
        if (bInRange&&bClick)
        {
            //获得位置
            currentPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            currentPosition.z = 0;//记得一定要设置,不然的话会出问题
            Debug.Log(currentPosition);

            //获得角度,超过最大角度就不移动
            var pos = Vector3.SignedAngle(Vector3.up, currentPosition - Arm.transform.position, Vector3.forward);//这里要注意signedangle的用法
                                                                                                                 // currentAngle = pos;
            currentAngle = (pos <= maxAngle && pos >= 0) ? pos : 0 ;          //: pos;

            //调整手臂旋转角度
            Arm.transform.eulerAngles = new Vector3(0, 0, currentAngle);
            



        }
        else
        {

            Arm.transform.eulerAngles = new Vector3(0, 0, 0);
        }
        //重置canrotate




    }

}

 

最后建立一个ShowWindow的square,为他添加rect transform,作为白色背景,调整到合适大小和想要的位置

ctrl+d 一个Brush的body,重命名为littlebrush,拖拽为其子物体 ,

 

 

 创建ShowWindow脚本,拖拽到ShowWindow上

 

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

public class ShowWindow : MonoBehaviour
{
    public Transform Brush;

    private Transform littleBrush;
    public float ratioWidth,ratioHeight;
    private void Awake()
    {
       // ratioWidth = GetComponent<RectTransform>().rect.width/Screen.width;
        //ratioHeight = GetComponent<RectTransform>().rect.height/Screen.height;
        littleBrush = transform.GetChild(0);
    }
    private void Update()
    {
        littleBrush.localPosition = new Vector3(Brush.position.x * ratioWidth,Brush.position.y * ratioHeight, Brush.position.z) ;
    }
}

 大功告成!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值