Unity 限制范围-限制在4分之一圆柱形范围内活动

news/2024/10/18 8:33:10/

在我的游戏中,玩家的两只手操控中,不想让他们的手围着自己在一个圆形范围内随便乱跑,左手只想让他在左上角,右手在右上角范围活动。所以我制作这样一个算法来实现。
在这里插入图片描述

首先用Dot函数划分出4个区域,然后根据区域计算修正后的位置。
先上代码:
hero就是图中蓝色胶囊,hand是黄色的球,红色的newhand是最后修正的位置。如果两个球重叠表示不需要修正,如果修正了红色就是真实算法位置。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class testRange : MonoBehaviour
{public Transform hero;  //角色位置public Transform hand;  //模型手public Transform newhand;   //修正后的位置public float maxDis = 0.6f;protected void Update(){Vector3 dir;//让左手在第二象限。dot是0-1,dot2是-1~0Vector3 atHero = hero.position;Vector3 atHand = hand.position;atHero.y = atHand.y;dir = (atHand - atHero).normalized;float dot = Vector3.Dot(hero.forward, dir);float dot2 = Vector3.Dot(hero.right, dir);Debug.Log(dot + "," + dot2);Vector3 nowat = hand.position;if (dot < 0f){//hand超过身后nowat = GetIntersectWithLineAndPlane(nowat, hero.forward, hero.forward, hero.position);}if (dot2 > 0f){//hand超过右边nowat = GetIntersectWithLineAndPlane(nowat, hero.right, hero.right, hero.position);}//只能围绕身边Vector3 heroat = hero.position;heroat.y = nowat.y;float dis = Vector3.Distance(nowat, heroat);if (dis < maxDis){Vector3 todir = (nowat - heroat).normalized;if (!todir.Equals(Vector3.zero))nowat = heroat + todir * maxDis;else{todir = (heroat - hand.position).normalized;nowat = heroat + todir * maxDis;}}newhand.position = nowat;}/// <summary>/// 计算直线与平面的交点/// </summary>/// <param name="point">直线上某一点</param>/// <param name="direct">直线的方向</param>/// <param name="planeNormal">垂直于平面的的向量</param>/// <param name="planePoint">平面上的任意一点</param>/// <returns></returns>private Vector3 GetIntersectWithLineAndPlane(Vector3 point, Vector3 direct, Vector3 planeNormal, Vector3 planePoint){float d = Vector3.Dot(planePoint - point, planeNormal) / Vector3.Dot(direct.normalized, planeNormal);return d * direct.normalized + point;}
}

上面的代码就是计算hand在第二象限的限制,首先我们观察到dot 在第二象限是 0 ~ 1 的范围,dot2是 -1 ~ 0 ,当超过这个范围了,我们可以通过GetIntersectWithLineAndPlane函数来计算超过这个结界的碰撞点,防止出界。

最后我们可能不希望他跑到玩家某型里面了,我们给一个半径范围maxDis ,让他不要和玩家重叠。
看看效果图。
请添加图片描述

当我们移动到背后的时候,小球最多和我们平行,当小球移动到右边的时候那么最多知道面前,而且不会进入玩家模型范围。

相交算法链接来自


http://www.ppmy.cn/news/860352.html

相关文章

h3c S2000-EA 交换机DHCP Snooping支持Option 82功能的配置

一、 组网需求: Switch 的端口Ethernet1/0/5与DHCP 服务器端相连,端口Ethernet1/0/1,Ethernet1/0/2,Ethernet1/0/3分别与DHCP Client A、DHCP Client B、DHCP Client C相连。 (1)在Switch上开启DHC

富士在海外发表数码相机--「FinePix S2000HD」

富士フイルム 在海外发表了搭载光学 15 倍 变 焦距镜头的数码相机 「 FinePix S2000HD 」。 9 月在美国发售 299.95 美元 。 具备自广角 27.6mm (35mm 换算的 15 倍 焦距高倍率模式 。 摄像素子是 1/2.3 型有效 1,000 万象素 CCD 。 最大记录解像度是 3,6482,736 象素 。 感度是…

s2000gc65魔术数

讲解前的废话&#xff1a;这道题太难了吧&#xff0c;我整了一坤年才整出来&#xff08;Bushi 废话就讲到这里&#xff0c;切入正题&#xff01;&#xff01;&#xff01; 5、魔术数(e.cpp) 【题目描述】 X是一个正整数&#xff0c;它的第i个数字是d[i]&#xff0c;则 序列…

s2000gc63字典序

3、字典序(c.cpp) 【题目描述】 有一个字符串S&#xff0c;S的每个字符都是小写字母。有N个贴纸&#xff0c;第i个贴纸上面印有一个字符ch[i]。使用1个贴纸可以替代S的1个字符。例如你可以使用第i个贴纸去替代S的第j个字符&#xff0c;即把S[j]替换成ch[i]。注意&#xff1a;贴…

C++ s2000gc64独居

第4题 s2000gc64独居 时限&#xff1a;1s 空间&#xff1a;256m 4、独居(d.cpp) 【题目】 一只奶牛自己独居&#xff0c;它每天需要吃1个水果和花费X元交暖气费。现在奶牛家里有F个水果和D元。奶牛最多能独居多少天&#xff1f;奶牛可以去超市买水果&#xff0c;超市每…

s2000gc66存在

6、存在(f.cpp) 【题目描述】 给出一个B数组B[0...n-1]。你要寻找另一个数组A[0...n-1]&#xff0c;使得A数组同时满足如下条件&#xff1a; 1、对于0<i<n&#xff0c; A[i]必须是2^j&#xff0c;其中j是正整数。即A[i]必须是2,4,8,16,32,64...等这样的数。 2、对于0&…

ATX电源维修实例

<script type"text/javascript"> </script> <script type"text/javascript" src"http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 一天因为我反复开机几次&#xff0c;后来按电源键居然没有反应了…