一, 场景的模拟
情景介绍:
当子弹子打中物体, 会在其物体上留下单孔, 我以弹皮模拟
二, 脚本
脚本挂载在弹皮上
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()
{
}
}