So I am trying to have my energy sword cast a constant ray to check if an enemy is within range. However when I turn on the debug render I cannot get the raycast ray to show. I’ve been able to set this up for other parts of my code with no issues. Some of the code has been moved around for debugging.
void EnergySword::RegisterObject(Context* context)
{
context->RegisterFactory<EnergySword>();
}
void EnergySword::Start()
{
loadResources();
// Component has been inserted into its scene node. Subscribe to events now
SubscribeToEvent(GetNode(), E_NODECOLLISION, URHO3D_HANDLER(EnergySword, HandleNodeCollision));
}
void EnergySword::DelayedStart()
{
SubscribeToEvent(node_->GetChild("right_hand_marker", true), E_POSTRENDERUPDATE, URHO3D_HANDLER(EnergySword, MeleeLunge));
SubscribeToEvent(GetNode(), E_ANIMATIONTRIGGER, URHO3D_HANDLER(EnergySword, HandleAnimationTriggerZombie));
}
void EnergySword::FixedUpdate(float timeStep)
{
}
void EnergySword::loadResources()
{
cache = GetSubsystem<ResourceCache>();
node_->SetScale(Vector3(0.025f, 0.025f, 0.025f));
swordObject = node_->CreateComponent<AnimatedModel>();
swordObject->SetModel(cache->GetResource<Model>("Models/plasma_sword.mdl"));
swordObject->ApplyMaterialList("Materials/plasma_sword.txt");
meleeshape_ = node_->CreateComponent<CollisionShape>();
meleeshape_->SetBox(Vector3(50.0f, 20.0f, 2.0f), Vector3::ZERO, Quaternion::IDENTITY);
}
void EnergySword::HandleNodeCollision(StringHash eventType, VariantMap& eventData)
{
// Check collision contacts and see if character is standing on ground (look for a contact that has near vertical normal)
using namespace NodeCollision;
RigidBody* body = node_->GetComponent<RigidBody>(true);
MemoryBuffer contacts(eventData[P_CONTACTS].GetBuffer());
while (!contacts.IsEof())
{
Vector3 contactPosition = contacts.ReadVector3();
Vector3 contactNormal = contacts.ReadVector3();
/*float contactDistance = */contacts.ReadFloat();
/*float contactImpulse = */contacts.ReadFloat();
// If contact is below node center and pointing up, assume it's a ground contact
if (contactPosition.y_ < (node_->GetPosition().y_ + 1.0f))
{
float level = contactNormal.y_;
}
}
}
void EnergySword::PlaySound(const String& soundName, float gain, float nearAttenuation, float farAttenuation)
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
// Create the sound source node
Node *sound_node = node_->CreateChild("Sound source node");
Sound* sound = cache->GetResource<Sound>(soundName);
soundSrc = node_->CreateComponent<SoundSource3D>();
soundSrc->SetNearDistance(nearAttenuation);
soundSrc->SetFarDistance(farAttenuation);
// Create the listener node
Node *listener_node = scene_->GetChild("Player", true);
// Create the listener itself
SoundListener *listener = listener_node->CreateComponent<SoundListener>();
// Set the listener for that audio subsystem
cache->GetSubsystem<Audio>()->SetListener(listener);
soundSrc->SetGain(gain);
soundSrc->Play(sound);
soundSrc->SetAutoRemoveMode(REMOVE_COMPONENT);
}
void EnergySword::HandleAnimationTriggerZombie(StringHash eventType, VariantMap& eventData)
{
using namespace AnimationTrigger;
AnimatedModel* model = node_->GetComponent<AnimatedModel>();
if (model)
{
AnimationState* state = model->GetAnimationState(eventData[P_NAME].GetString());
if (eventData[P_NAME].GetString() == "Melee 002")
{
bone = node_->GetChild(eventData[P_DATA].GetString(), true);
if (bone != NULL)
{
PlaySound("Sounds/sword_melee%2.ogg", 0.5f, 0.1f, 10.0f);
}
}
if (state == NULL)
{
return;
}
}
}
void EnergySword::DrawDebug(DebugRenderer* debug, bool depthTest)
{
Vector3 size_{ 50.0f, 20.0f, 2.0f };
Vector3 startPos_{};
Vector3 endPos_{};
Quaternion startRot_{};
Quaternion endRot_{};
const BoundingBox bounds{ -size_ * .5f, size_ * .5f };
debug->AddBoundingBox(bounds, { startPos_, startRot_, 1.f }, Color::GREEN, depthTest);
debug->AddBoundingBox(bounds, { endPos_, endRot_, 1.f }, Color::RED, depthTest);
debug->AddLine(startPos_, endPos_, Color::YELLOW, depthTest);
}
void EnergySword::MeleeLunge()
{
PhysicsWorld* physicsWorld_ = scene_->GetComponent<PhysicsWorld>();
PhysicsRaycastResult result;
Vector3 pos(node_->GetWorldPosition());
Ray ray(pos, node_->GetWorldDirection()); // the given vector is the direction
physicsWorld_->RaycastSingle(result, ray, 40.0f, 1);
debug->AddLine(pos, Vector3(0, 0, 0), Color::RED, false);
if (result.distance_ <= 40)
{
}
}