Unity3D之弹皮紧贴效果实现
  RlWeLU85QNwT 2023年11月02日 90 0

一, 场景的模拟

Unity3D之弹皮紧贴效果实现_弹皮

情景介绍:

当子弹子打中物体, 会在其物体上留下单孔, 我以弹皮模拟


二, 脚本

脚本挂载在弹皮上

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

public class NomalDemo : MonoBehaviour
{
[SerializeField]
private GameObject ground;//地面
void Start()
{
this.StickOnGround();
}
/// <summary>
/// 贴在地面上
/// </summary>
private void StickOnGround()
{
RaycastHit hitInfo;//用来得到射线碰撞的信息
Vector3 capsuleColliderCenterInWorldSpace =
GetComponent<CapsuleCollider>().
transform.TransformPoint(GetComponent<CapsuleCollider>().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()
{

}
}

三, 结果

Unity3D之弹皮紧贴效果实现_弹皮_02

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

RlWeLU85QNwT