Unity3D之弹皮紧贴效果实现

一, 场景的模拟 情景介绍: 当子弹子打中物体, 会在其物体上留下单孔, 我以弹皮模拟 二, 脚本 脚本挂载在弹皮上 using System.Collections; using System.Collections.Generic; using UnityEngine; public class NomalDemo : MonoBehaviour { [SerializeField] private GameObject ground;//地面 void Start() { this.StickOnGround(); } ///

/// 贴在地面上 ///

private void StickOnGround() { RaycastHit hitInfo;//用来得到射线碰撞的信息 Vector3 capsuleColliderCenterInWorldSpace = GetComponent(). transform.TransformPoint(GetComponent().center);//得到胶囊的中心点 bool isHit = Physics.Raycast( capsuleColliderCenterInWorldSpace, new Vector3(0f, -1f, 0f), out hitInfo, 100f, LayerMask.GetMask("Default") );//从胶囊的中心垂直向下发射射线 if (isHit) { Vector3 forward = this.transform.forward; Vector3 left = Vector3.Cross(forward, hitInfo.normal);//确定左的方向 Vector3 newForward = Vector3.Cross(hitInfo.normal, left);//新的正方向 this.transform.rotation = Quaternion.LookRotation(newForward, hitInfo.normal); this.transform.position = hitInfo.point;//设置位置,紧贴地面 } } // Update is called once per frame void Update() { } } 三, 结果

提供优质的网站源码大全,小程序、APP、H5、支付、游戏、区块链、商城、直播、影音、小说、公众号等源码下载。
易搜网络技术公司 » Unity3D之弹皮紧贴效果实现
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡