Start/stop ability via script

Ando5000

Member
Working on a basic stealth ability. Currently im using the basic crouch (height change) ability and a large sphere collider that is set to not collide with anything but another sphere collider placed on the enemy. when the player gets to close they will be discovered by a trigger and fire a script on the player. I would like it to stop the crouch ability on trigger enter.

private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
var sneakAbility = characterLocomotion.GetAbility<HeightChange>();

characterLocomotion.TryStopAbility(sneakAbility, true);
Debug.Log("caught me, sneak failed");
}
}

Debug.Log Fires, but Crouch ability continues. I attempted to debug this in the TryStopAbility() method and the log says crawl is not active. I assume both crawl and crouch are using the same ability? How do i force it to return to the standing state?

Log:
not activeOpsive.UltimateCharacterController.AddOns.Agility.Crawl
UnityEngine.Debug:Log(Object)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion:TryStopAbility(Ability, Boolean) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1406)
StealthDetection:OnTriggerEnter(Collider) (at Assets/StealthDetection.cs:22)
 

Ando5000

Member
After more testing this works fine with other abilities. I may just need to create a new ability instead of using (crouch)heightchange , since it appears to be fairly generic.
 

Andrew

Moderator
Staff member
TryStopAbility on the HeightChange ability works fine for me on the default crouch ability in the demo scene, although the Agility pack crawl ability may be set up a little different. I don't have access to it right now, but the crawl ability may have a different component name? Or as you suggested, using a custom ability and state may be simpler.
 

Andrew

Moderator
Staff member
If there is more than one ability of the same type, you can specify one of them using its ability index by passing the index as an argument to GetAbility, like so:

characterLocomotion.GetAbility<HeightChange>(abilityIndex);
 

Ando5000

Member
Turns out if crawl was above height change in the ability list characterLocomotion.TryStopAbility(sneakAbility, true); would result in crawl instead of crouch. when i moved crawl down it fired crouch, as expected.

I ended up copying height change script, renaming the script to crouch and adding it to ability list. There hasn't been any conflict this way and i think better for the long run.

Thanks for the help.
 
Top