On trigger speed change

Silver100

Member
Hi,

Im trying to get on trigger (after contact with object) speed change to work by comparing API abilty but won't seem to funtion for some reason as below:

Code:
using UnityEngine;// note this is just an experiment before script below
using Opsive.UltimateCharacterController.Character.Abilities;//new
using Opsive.UltimateCharacterController.Character;
public class Shoespeed :Ability// Experiment

{

   [Tooltip("Your character title goes here")]//
    [SerializeField] protected GameObject m_Character;//

    public override void OnTriggerEnter(Collider other)//
    {
        if (other.tag == "Shoespeed") // character makes contact with a pair of shoes assigning new shoes to player through another script on shoes  then speed should alter here

        {
            var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
            var speedAbility = characterLocomotion.GetAbility<SpeedChange>(35);// just a guess here?

            //GetComponent<UltimateCharacterLocomotion>().MotorAcceleration.z.Equals(80);// wont work this way
            //GetComponent<UltimateCharacterLocomotion>().MotorAcceleration.x.Equals(80);// wont work this way
            characterLocomotion.TryStartAbility(speedAbility);
        }
    }
}
 
Last edited:
Can you please use the [ code ] tag so it's easier to read? Thank you!

From the character controller side of things things look ok. Does TryStartAbility get called? If you place a breakpoint and step into that method where does it return early?
 
Thanks, Oops I just realised I need to be adding code][/code and now get it, I have not used breakpoints and debug yet but I will try figuring that out.

Code:
characterLocomotion.TryStartAbility(speedAbility). Equals(20);
// Tried this too but not working
Also tried directly in the Speedchange script with the on trigger statement.

By default pressing the shift button activates speed using the 'speedchange' script - so if I could somehow alter this script adding speed & on trigger logic instead, so the player changes speeds passing through an object or basically like a speed zone.

In the inspector under Speed Change ability - (SpeedChange script handles this)
You can manually increase the changed speed; (when shift or assgned button is pressed player runs).

Change speed multiplier=
Max speed change=
Speed parameter value=

Also using
Code:
 GetComponent<SpeedChange>().enabled = false;
I was'nt able to get this to work either.

Would you be able to release a simple speed trigger API? I have tried to create one in many different ways using API ability example format - usually I can arrange and get most ability's working not speed though.
 
Last edited:
You will need to call TryStartAbility. SpeedChage isn't a Component so you cannot call GetComponent with it. If TryStartAbility isn't working I would place a breakpoint within that method and see what condition it is failing on. This will give you a better idea of the cause.
 
Thanks, this is working well. Looks like a Null ref was the error (script not locating player) (I used a code breakpoint) I was setting up as a new ability in error. I should be calling an existing ability and changing with Mono behaviour. Now its getting called. I am struggling with a method to keep the running animation constant in a zone though. But if its just to increase the players speed in a zone similar to the demo then this method is perfect.

In the demo the shift button needs to be held down, even in the increased speed zone area which seems logical, this method would be ideal but not sure how to set it that way? Tried changing ability priority order & different start stop button methods.

**I have tried experimenting duplicating SpeedChange name SpeedChange2 but with start button down stop button down too. Having both abilities active but SpeedChange2 in the script below instead. This would give 2 speed set ups? for some reason though running animation stops & returnsto normal in the zone if no vertical axis movement)**

****This method below works well increasing the speed of the player in a zone but not sustaining running**** animation
Code:
using UnityEngine;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.Abilities;

public class Speedchanger : MonoBehaviour//
{
    [Tooltip("The character that should start and stop the speedchange ability.")]
    [SerializeField] protected GameObject m_Character;
    /// <summary>
    /// Starts and stops the Speedchange ability.
    /// </summary>
 
    void OnTriggerEnter(Collider other)//
    {

        if (other.tag == "Speedzone") //
        {

            var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
            var speedchangeAbility = characterLocomotion.GetAbility<SpeedChange>(); // or  could try SpeedChange2 here with altered speeds? would need method to switch here. / toggle method?
                                                                                    //
                                                                                    // such as if it doesn't have a high enough priority or if CanStartAbility returns false.
            characterLocomotion.TryStartAbility(speedchangeAbility);// or change toTryStopAbility

            {
                characterLocomotion.TryStartAbility(speedchangeAbility); //change to stop for SpeedChange2?
                characterLocomotion.MotorAcceleration = new Vector3(90, 0, 90);// increased speed

            }
        }
    }
        void OnTriggerExit(Collider other)//
           {// exits zone

            if (other.tag == "Speedzone") //
            {
                var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
                var speedchangeAbility = characterLocomotion.GetAbility<SpeedChange>(); // or enter SpeedChange2 here with altered speeds would need method to switch here. / toggle method
                                                                                        //
                                                                                        // such as if it doesn't have a high enough priority or if CanStartAbility returns false.
                characterLocomotion.TryStartAbility(speedchangeAbility);// or change toTryStopAbility

                {
                    characterLocomotion.TryStartAbility(speedchangeAbility); //or change toTryStopAbility
                    characterLocomotion.MotorAcceleration = new Vector3(20, 0, 20); //return to normal speed
 
               }
            }
        }
    }

}
 
Last edited:
Top