简体   繁体   English

统一。 与法线的角度。 “打孔能力”的线性变化

[英]Unity. Angle to the normal. Linear change of “punching capacity”

How to find the angle to the normal, under which the bullet hit the surface? 如何找到子弹撞击表面的法线角度? And a bigger question, how to make a linear change of the "punching capacity" of a bullet depending on this angle? 还有一个更大的问题,如何根据该角度线性改变子弹的“打孔能力”? That is, so that at an angle of 45 degrees, the bullet lost half of the "punching ability"? 也就是说,子弹以45度角失去了一半的“打孔能力”? Multiplying by standard trigonometric functions makes no sense, for they are all non-linear. 用标准三角函数相乘是没有意义的,因为它们都是非线性的。 Help please, because I do not understand trigonometry at all ... 请帮助,因为我根本不了解三角函数...

The only thing, which I can find now - is the normal to the surface of the hit. 我现在能找到的唯一东西是击球表面的法线。 enter image description here 在此处输入图片说明

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

public class Bullet : MonoBehaviour
{
public float BulletImpulse { get; private set; }
public Rigidbody BulletRigidbody { get; private set; }

void Start()
{
    BulletImpulse = 3f;
    BulletRigidbody = GetComponent<Rigidbody>();
    BulletRigidbody.AddForce(transform.up * BulletImpulse, ForceMode.Impulse);
    BulletRigidbody.AddTorque(-transform.up * BulletImpulse, ForceMode.Impulse);
}

void OnCollisionEnter(Collision collision)
{
    foreach (var item in collision.contacts)
    {
        Debug.DrawLine(item.point, item.point + item.normal, Color.green, 100, false);
        break;
    }
}}

To get the angle, you will need the direction of your bullet, which would be the normalized direction of the velocity at impact, and the direction of the normal at the point of impact. 为了获得角度,您将需要子弹的方向,这是撞击速度的归一化方向,以及撞击点的法线方向。 You can then use Vector3.angle() to get the angle between the two, and multiply that by the impact factor to get your result. 然后,您可以使用Vector3.angle()获取两者之间的角度,并将其乘以影响因子即可获得结果。

void OnCollisionEnter(Collision collision)
{
   Vector3 bulletDir = collision.getComponent<Rigidbody>().velocity.normalized;
   Vector3 collNormal = collision.contacts[0].normal;
   float angle = Vector3.angle(collNormal, bulletDir);

   float impactFactor = 1 - angle/90f; // impact on a scale from 0-1;
}

(You may need to use the inverse of the bulletDir) (您可能需要使用bulletDir的反函数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM