homing projectiles
This commit is contained in:
@@ -68,18 +68,44 @@ public partial class Projectile : RigidBody3D, IForgeEntity, ITargetable, IKilla
|
||||
[Node("ForgeEntityNode")] public required ForgeEntityNode ForgeEntity { get; set;}
|
||||
|
||||
[Export] public float Damage = 10f;
|
||||
[Export] public float Speed = 10f;
|
||||
[Export] public float HomingFactor = 0.2f;
|
||||
|
||||
public Node3D Target { get; set; }
|
||||
public Node3D? Target { get; set; }
|
||||
|
||||
public void Init()
|
||||
{
|
||||
ApplyCentralImpulse(Vector3.Up * 10);
|
||||
ApplyCentralImpulse(ComputeImpulseToTarget());
|
||||
}
|
||||
|
||||
public Vector3 ComputeImpulseToTarget()
|
||||
{
|
||||
if (Target == null) return Vector3.Zero;
|
||||
return Vector3.Up * Speed;
|
||||
|
||||
var targetDir = GlobalPosition.DirectionTo(Target.GlobalPosition);
|
||||
var hDir = new Vector3(targetDir.X, 0, targetDir.Z).Normalized();
|
||||
var throwDir = (hDir + 3*Vector3.Up).Normalized();
|
||||
var distanceTo = GlobalPosition.DistanceTo(Target.GlobalPosition);
|
||||
|
||||
return throwDir * distanceTo*0.8f;
|
||||
}
|
||||
|
||||
public override void _IntegrateForces(PhysicsDirectBodyState3D state)
|
||||
{
|
||||
var direction = GlobalPosition.DirectionTo(Target.GlobalPosition);
|
||||
state.LinearVelocity += direction / 10.0f;
|
||||
if (Target == null) return;
|
||||
var targetPos = Target is ITargetable targetable ? targetable.GetTargetGlobalPosition() : Target.GlobalPosition;
|
||||
var targetVel = state.LinearVelocity + GlobalPosition.DirectionTo(targetPos) * HomingFactor;
|
||||
if (targetVel.Length() > Speed) targetVel = targetVel.Normalized() * Speed;
|
||||
state.LinearVelocity = targetVel;
|
||||
// var targetDir = GlobalPosition.DirectionTo(targetPos);
|
||||
// var hDir = new Vector3(targetDir.X, 0, targetDir.Z).Normalized();
|
||||
// var currentVelocityHDir = new Vector3(state.LinearVelocity.X, 0, state.LinearVelocity.Z);
|
||||
// var finalHDir = hDir*HomingFactor + currentVelocityHDir;
|
||||
// var combinedVelocity = new Vector3(finalHDir.X, state.LinearVelocity.Y, finalHDir.Z);
|
||||
// if (combinedVelocity.Length() > Speed) combinedVelocity = combinedVelocity.Normalized() * Speed;
|
||||
// state.LinearVelocity = combinedVelocity;
|
||||
// state.LinearVelocity += targetDir/5.0f;
|
||||
}
|
||||
|
||||
public void OnResolved()
|
||||
|
||||
@@ -55,7 +55,7 @@ script = ExtResource("12_6slh1")
|
||||
|
||||
[sub_resource type="Resource" id="Resource_uh7hr"]
|
||||
script = ExtResource("12_6slh1")
|
||||
BaseValue = 10.0
|
||||
BaseValue = 1.0
|
||||
metadata/_custom_type_script = "uid://cn3b4ya15fg7e"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_ckp55"]
|
||||
@@ -201,6 +201,8 @@ continuous_cd = true
|
||||
contact_monitor = true
|
||||
max_contacts_reported = 1
|
||||
script = ExtResource("1_bwggc")
|
||||
Speed = 20.0
|
||||
HomingFactor = 0.8
|
||||
|
||||
[node name="ForgeEntityNode" type="Node3D" parent="." unique_id=1961065393]
|
||||
script = ExtResource("3_0m0gv")
|
||||
|
||||
Reference in New Issue
Block a user