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() { } } 三, 结果

尚美源码坑位教程提供精美的网站源码教程,小程序、公众号、H5、APP、游戏、直播、支付、区块链、商城、影音、小说等源码信息大全。
尚美源码教程库 » Unity3D之弹皮紧贴效果实现
赞助VIP 享更多特权,立即登录下载海量资源
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡