简体   繁体   English

使用自定义力量进行弹丸射击

[英]Projectile shooting with custom force

I have made a projectile shooting system.我做了一个弹丸射击系统。 I want to shoot an object from position A to B following an indicated path.我想按照指示的路径从 position A 到 B 拍摄 object。 Everything is working fine except for one thing.除了一件事,一切都很好。 The velocity applied on the object is calculated based on the distance between A and B, the value of time is 1 to travel this distance. object 上应用的速度是根据 A 和 B 之间的距离计算的,时间的值为 1 来移动这个距离。 Meaning that the farther I hit, the quicker it goes.这意味着我打得越远,它走得越快。 I want to have control of the force applied.我想控制施加的力。 Meaning that it should go with my set speed whether I hit near or far.这意味着无论我打近还是远,它都应该以我设定的速度 go 。 Tried normalizing the velocity and multiplied it by my custom force value, but then it moves away from its trajectory.尝试标准化速度并将其乘以我的自定义力值,但随后它会远离其轨迹。 (See this link below, no matter how close or far we hit the object. It goes with the same speed while following the trajectory indicated. I want to develop this functionality. https://play.google.com/store/apps/details?id=com.ghakilo.trickytrack ) (请参阅下面的此链接,无论我们击中 object 的距离有多近或多远。它按照指示的轨迹以相同的速度运行。我想开发此功能。https://play.google.com/store/apps/详细信息?id=com.ghakilo.trickytrack

Vector3 calculateVelocity(Vector3 target, Vector3 origin, float time)
 {
     Vector3 distance = target - origin;
     Vector3 distanceXZ = distance;
     distanceXZ.y = 0f;

     float Sy = distance.y;
     float Sxz = distanceXZ.magnitude;
     float Vxz = Sxz / time;
     float Vy = 0f;
     
     Vy = Sy / time + 0.5f * Mathf.Abs(Physics.gravity.y) * time;
    
     Vector3 result = distanceXZ.normalized;
     result = result * Vxz;
     result.y = Vy;

     return result;
 }

Physics time!物理时间!

Velocity is a vector whose magnitude is speed.速度是一个矢量,其大小为速度。

If you want to fix the speed at which your projectile starts, that only leaves the direction of the velocity in your hands, so you need to calculate the direction in which you want to yeet your projectile.如果你想确定你的弹丸开始的速度,那只剩下你手中的速度方向,所以你需要计算你想要弹丸的方向。

If you're like me, you shoot straight at the target, so you'd set the direction vector to go from the origin to the target.如果你像我一样,你直接朝目标射击,所以你需要将方向矢量设置为从原点到目标的 go。 Which is simply target - origin , or what you calculate as distance .这只是target - origin ,或者你计算的distance I'm going to call this direction because that's what it really is being used for.我将称之为这个direction ,因为这就是它真正的用途。

Now to use this as the direction vector for your velocity, convert distance to a unit vector (not sure how you do this in the unity framework, but direction.Normalize() ?)现在将其用作速度的方向向量,将distance转换为单位向量(不确定在统一框架中如何执行此操作,但direction.Normalize() ?)

Then multiply this by the speed to get your velocity vector!然后乘以速度得到你的速度向量!

Vector3 calculateVelocity(Vector3 target, Vector3 origin, float speed)
 {
     Vector3 direction = target - origin;
     direction.Normalize();

     Vector3 result = direction * speed;

     return result;
 }

First add rigidbody to your bullet and set the gravity scale to.5首先为你的子弹添加rigidbody并将重力比例设置为.5

and add this code to your bullet prefab并将此代码添加到您的子弹预制件中

Vector3 calculateVelocity(Transform target, Transform origin, float time)
    {
        Vector3 direction = target.transform.position - origin.transform.position;
        float distance = Vector3.Distance(origin.transform.position, target.transform.position);
        result = direction.normalized * distance * speed * Time.deltaTime;
        return result;
    }

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

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