gpt4 book ai didi

c# - Unity - OnTriggerEnter 子弹跳弹

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:50 24 4
gpt4 key购买 nike

我在简单的弹道学方面遇到了一些问题。由于我是编码新手,而且我浪费了 2 天的时间进行搜索,所以我决定问一个问题。我的 bullet.cs 脚本有问题,代码工作正常,除了跳弹部分。我使用 OnTriggerEnter 因为子弹会穿透不同的 Material ,但会在特定角度弹跳,所以我只使用对撞机作为触发器。我知道可以使用 OnCollisionEnter 确定法线并使用 Vector3.Reflect。但是,如果对撞机没有设置为仅作为触发器,子弹就会从任何东西上弹开,永远不会穿透墙壁。只需要底部的弹跳部分的帮助。

public Rigidbody rb;

private float vel;
private float kEnergy;

private bool AP = false;
private bool HP = false;
private bool ricochet = false;

// Use this for initialization
void Start () {

vel = 738f;
kEnergy = 2108f;
//HP == false;
Destroy (gameObject, 10);
rb = GetComponent<Rigidbody> ();
rb.velocity = transform.forward * vel;
Quaternion bulletRotaion = transform.rotation;
Debug.Log (bulletRotaion.eulerAngles);
}

// Update is called once per frame
void Update () {

float curVelocity = rb.velocity.magnitude;
if (curVelocity <= 0)
{
curVelocity = 0f;
}
}

void OnTriggerEnter (Collider c)
{
if (c.tag == "Wall") {

Quaternion localOffset = transform.rotation;
float impactAngleX = c.gameObject.GetComponent <MaterialDensity> ().angleX;
float impactAngleY = c.gameObject.GetComponent <MaterialDensity> ().angleY;
float impactAngleZ = c.gameObject.GetComponent <MaterialDensity> ().angleZ;
float angleX = localOffset.eulerAngles.x;
float angleY = localOffset.eulerAngles.y;
float angleZ = localOffset.eulerAngles.z;
float density = c.gameObject.GetComponent <MaterialDensity> ().materialDensity;
float ricochetX = (angleX + impactAngleX);
if (ricochetX > 360)
{
ricochetX = (ricochetX - 360);
}

if (ricochetX < 0)
{
ricochetX = (ricochetX + 360);
}
float ricochetY = (angleY + impactAngleY);
if (ricochetY > 360)
{
ricochetY = (ricochetY - 360);
}

if (ricochetY < 0)
{
ricochetY = (ricochetY + 360);
}
float ricochetZ = (angleZ + impactAngleZ);
if (ricochetZ > 360)
{
ricochetZ = (ricochetZ - 360);
}

if (ricochetZ < 0)
{
ricochetZ = (ricochetZ + 360);
}

if ((ricochetX > 60 && ricochetX < 300) || (ricochetY > 60 && ricochetY < 300) || (ricochetZ > 60 && ricochetZ < 300)) {
ricochet = true;
//Debug.Log (ricochet);
}

if ((ricochetX < 60 && ricochetX > 300) || (ricochetY < 60 && ricochetY > 300) || (ricochetZ < 60 && ricochetZ > 300)) {
ricochet = false;
//Debug.Log (ricochet);
}

Debug.Log (ricochet);
Debug.Log (ricochetX);
Debug.Log (ricochetY);
Debug.Log (ricochetZ);

if (AP)
{
density *= 0.9f;
}
if (HP)
{
density *= 3f;
}

//float vel = gameObject.GetComponent<GunNew>().velocity;
float curVelocity = rb.velocity.magnitude;
float velocityMod = curVelocity / vel;
float densityMod = density / velocityMod;
float curEnergy = (kEnergy * velocityMod);
float energyMod = (curEnergy * velocityMod);
rb = GetComponent<Rigidbody> ();
//Quaternion localOffset = transform.rotation;
float randomX = Random.Range (0.03f, 0f);
float randomY = Random.Range (0.03f, -0.03f);
if (randomX == 0)
{
randomX = 0.01f;
}
if (randomY == 0)
{
randomY = 0.01f;
}
localOffset.x += randomX;
localOffset.y += randomY;

if (curVelocity > densityMod && !ricochet) {
rb.rotation = localOffset;
rb.velocity = transform.forward * ((curVelocity - (curVelocity * (randomX + randomY))) - densityMod);

//Debug.Log (curVelocity);
//Debug.Log (energyMod);
}

if (curVelocity > densityMod && ricochet)
{
Vector3 objAngle = new Vector3 (impactAngleX, impactAngleY, impactAngleZ);
Vector3 bulletAngle = rb.rotation.eulerAngles;
Debug.Log (objAngle);
}

if (curVelocity <= densityMod)
{
//Debug.Log (curVelocity);
//Debug.Log (energyMod);
Destroy (gameObject);
}
}
}

第二个代码是墙上的代码。

public float materialDensity = 100f;
public float angleX;
public float angleY;
public float angleZ;

//Quaternion localOffset = transform.rotation;

// Use this for initialization
public void Start ()
{
GetComponent<Rigidbody> ();
Quaternion localOffset = transform.rotation;
float angleX = localOffset.eulerAngles.x;
float angleY = localOffset.eulerAngles.y;
float angleZ = localOffset.eulerAngles.z;
//Debug.Log (localOffset.eulerAngles.x);
}

// Update is called once per frame
void Update () {

}

最佳答案

首先,我会在你的游戏中为会弹跳的东西和不会弹跳的东西制作一个图层。

然后,当您测试与子弹的碰撞时,您只能检查与弹跳层的碰撞。这将提高性能并使编码更简单。

从那里你可以依靠 unity 的物理来做它自己的跳弹,或者如果你想自己计算它你可以做一个光线转换并使用光线转换的法线,如上所述。

if(Physics.Raycast (shootRay, out shootHit, range, ricochetMask)) {...

关于c# - Unity - OnTriggerEnter 子弹跳弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com