Cube shaped gravity zones?

RAWilco

New member
Hi all.

I've been experimenting with another character controller recently in an attempt to achieve localised gravity zones using triggers.
I've been able to achieve this effect quite easily with rigidbody objects by simply setting Unity's global gravity to 0, 0 ,0 and then applying local forces to the rigidbodies as they enter the triggers. The character controller I've been using is also very basic (just movement and camera) and uses a non-kinematic rigidbody so it can be driven by forces. Currently I am reorienting the controller and camera when it enters a gravity trigger so that its up/down direction is the same as the trigger's Y direction.
However, I would very much like to replace this basic controller with UFPS and I realise that it won't simply be a case of swapping them out.

After a bit of playing around with the demo scene I'm starting to think that you're own Gravity Zone system may prove to be the answer.

I have a couple of questions though:

Currently the Gravity Zones are spherical and when a player is within one their local gravity is applied toward the centre of the sphere. Would it be possible to modify this ability so that instead of aligning towards the direction of a sphere's centre, it could instead align toward the local Y direction of a cube or mesh trigger instead?

And also, would it be possible to update this local gravity direction every frame? So that if the rotation of the gravity trigger were to change then the player's gravity and alignment would change with it?

I admit I have not spent an extensive amount of time diving into the ability system too deeply just yet, so I apologise if I may have missed something obvious. But any advice you could offer would be very much appreciated.
 
SphericalGravityZone is actually just an extension of the GravityZone component. So you could create your own simple CubeGravityZone component, functioning similarly to SphericalGravityZone but calculating the gravity direction differently (see the DetermineGravityDirection method in SphericalGravityZone).

You may also be interested in AlignToGround - a component which can be used to align the character's upright angle (and therefore gravity direction) to the angle of the ground underneath it. For example, this is what allows the character to walk smoothly across the ramp at the entrance to the gravity zone area in the demo scene whilst maintaining a perpendicular gravity direction to the slope at all times.
 
I cannot believe how simple that was.

Since posting my question I've been fiddling around with the AlignToGravity ability to see if I could make a new ability that would take into account the player's gravity on the character locomotion component (after it had been changed manually by a script on the gravity trigger). But while I was doing all that it did not occur to me to try modifying the existing Gravity Zone component (seems obvious now).

All I had to do was remove the lines of code regarding the Sphere Collider and Influence multiplier, then set the gravity direction to transform.up and it now does exactly what I wanted. It also works with any type of trigger as well.

I actually did play around a little with AlignToGround as well, but since it makes the player always align to the ground normal it made the camera behave a little awkwardly if there was any complex ground geometry such as bumps or ramps within the gravity zone (eg if there's a slope within the trigger, the player would align to the slope's normal, rather than the gravity direction). But that's not an issue with the modified gravity zone component.

Here's the code if anyone is interested (not that this is really needed because it was a very simple job to modify):

Code:
namespace Opsive.UltimateCharacterController.Objects.CharacterAssist
{
    using Opsive.UltimateCharacterController.Utility;
    using UnityEngine;

    public class SimpleGravityZone : GravityZone
    {

        private Transform m_Transform;
        private void Awake()
        {
            m_Transform = transform;
        }

        public override Vector3 DetermineGravityDirection(Vector3 position)
        {
            var direction = transform.up;
            return direction.normalized;
        }
    }
}

Thanks very much for the help.
 
@Andrew @RAWilco this might work for my question for similar thing.

Trying to keep players feet on the 'ground' of a spaceship while it undergoes rotation and movement in all directions.

ANyway to make it a stronger force? I'ts not really doing the job for me atm
 
Actually it works if I 'reset' the player.

eg. I'm testing by 'entering' and 'exit' the cockpit of the vehicle, at which point I guess the Locomotion is re-engagin to the collider's current position as the 'new' gravity direction (I think)

How could I make this script constantly update the direction - I only know how to use 'Bolt' visual scripting, not idea about C#
 
I also tried changing the line in the script :
private void Awake() to private void Update()

but didn't make a difference that I could see..
 
Top