Best way to handle on trigger event gravity change

Silver100

Member
Hi,

I have added height damage to my player when falling from a certain height. The floor is a trigger collider for damage to player when enemy is up on a high point gets activated and switched off just after player dies. I was trying to figure a way to have a higher gravity speed when high up so it looks more dramatic as it falls. I was thinking on trigger event at peak height, ceate the new ability / Gravity added to the Locomotion Character Controller

using Opsive.UltimateCharacterController.Character.Abilities;

Then apply some AddForce code. Is this the best approach for change of gravity during game or can I change the actual gravity value rather than apply additinal force? Will Opsive align to gravity zone do this?

 
Last edited:
Since you just want to change the speed at which the character falls I would instead adjust the Gravity Magnitude property on the Character Locomotion component. If you call AddForce then it won't realign that external force when in a gravity zone.
 
Thanks is there a way to adjust / call the function Gravity Magnitude in runtime so I can have variation to stronger and weaker gravity in different situations?
 
Yes, you can modify the GravityMagnitude on the grenade. In fact, this is perfect for the modular item system in that you would create a new module which adjusts the magnitude of the grenade.
 
Ah, oops. It is easier than - when the character enters a trigger you can adjust the GravityMagnitude on the CharacterLocomotion component.
 
No problem, Not sure I can cope with grenades just yet. Thanks I got it working by creating and adding this to the player or through ability system; Hopefully I'm doing this correctly.

Either this way;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Opsive.UltimateCharacterController.Character;

public class Gravitytrigger : MonoBehaviour
{

void OnTriggerEnter(Collider other)//
{
if (other.tag == "Gravitytest")// Select a gameobject i.e cube and tag it with 'Gravitytest' this adding a trigger collider too
{
GetComponent<UltimateCharacterLocomotion>().GravityAmount = (1f);// Magnitude could'nt locate, GravityAmount is woking fine
}
}
}

//GravityAmount=(0.2f); // usual
//GravityAmount=(1f); // extreme
//GravityAmount=(-10f);// flight

******************************************* Or through the ability system;// add below then click on ability drop down click plus sign search on Character Locomotion inspector 'Gravitytrigger then tick.

using UnityEngine;// This script does not need to be attached to any object unlike a mono script
using Opsive.UltimateCharacterController.Character.Abilities;//new
using Opsive.UltimateCharacterController.Character;
public class Gravitytrigger : Ability// name Gravitytrigger or what ever you want to name it here *

{

[Tooltip("Yourcharacter")]//
[SerializeField] protected GameObject m_Character;

public override void OnTriggerEnter(Collider other)
{
if (other.tag == "Gravitytest")// Select a gameobject i.e cube and tag it with this adding a trigger collider too
{
GetComponent<UltimateCharacterLocomotion>().GravityAmount = (1f);
}
}
}
 
Last edited:
Top