GrivityZone issue

yanghanwen

New member
Hi,I have an issue with GravityZone,I want to make rooms with different grivity direction.

Here are three of my problem:
1,when I set the gravity to (0,1,0) it doesn't work
2,when I enter the room,no matter how I slow the character will has a high velocity like I jump into the room
3,when I enter the room I can set the Ability AligntoGround to false,but when I exit the room I can't set the AligntoGround to true and AlignToGravityZone to false

Here's my code,


namespace Opsive.UltimateCharacterController.Objects.CharacterAssist
{
using Opsive.Shared.StateSystem;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.Abilities;
using Opsive.UltimateCharacterController.Game;
using Opsive.UltimateCharacterController.Utility;
using UnityEngine;

/// <summary>
/// The Room Gravity Zone applies a fixed gravity direction when the player enters a specific room.
/// </summary>
public class RoomGravityZone : GravityZone
{
[Tooltip("The fixed gravity direction to be applied in this room. It should be normalized.")]
[SerializeField] private Vector3 m_GravityDirection = Vector3.down;

protected string leaveRoomState= "AlignToGround";

protected string enterRoomState= "AlignToGravityZone";

/// <summary>
/// Ensures the gravity direction is normalized.
/// </summary>
private void Awake()
{
if (m_GravityDirection.magnitude == 0)
{
Debug.LogWarning("Gravity direction cannot be zero. Defaulting to Vector3.down.");
m_GravityDirection = Vector3.down;
}
else
{
m_GravityDirection.Normalize();
}
}

/// <summary>
/// Determines the direction of gravity that should be applied.
/// </summary>
/// <param name="position">The position of the character.</param>
/// <returns>The direction of gravity that should be applied.</returns>
public override Vector3 DetermineGravityDirection(Vector3 position)
{


Debug.Log($"Applying gravity direction: {m_GravityDirection}");
return m_GravityDirection;
}

private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, transform.position + m_GravityDirection);
}


protected override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
if (!string.IsNullOrEmpty(leaveRoomState))
{
var characterLocomotion = other.GetComponentInParent<UltimateCharacterLocomotion>();
StateManager.SetState(characterLocomotion.gameObject, leaveRoomState, false);
}
}
protected override void OnTriggerExit(Collider other)
{
// Call the base functionality first.
if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character))
{
return;
}


// The object must be a character.
var characterLocomotion = other.GetComponentInParent<UltimateCharacterLocomotion>();
if (characterLocomotion == null)
{
return;
}

// With the Align To Gravity Zone ability.
var alignToGravity = characterLocomotion.GetAbility<AlignToGravityZone>();
if (alignToGravity == null)
{
return;
}
Debug.Log("out");


alignToGravity.UnregisterGravityZone(this);
Debug.Log(alignToGravity.m_GravityZoneCount + "m_GravityZoneCount");
if (alignToGravity.m_GravityZoneCount == 0)
{
Debug.Log("down"+ string.IsNullOrEmpty(enterRoomState)+" "+ enterRoomState+ string.IsNullOrEmpty(leaveRoomState)+" "+ leaveRoomState);
if (!string.IsNullOrEmpty(enterRoomState))
{
Debug.Log("G1");
StateManager.SetState(characterLocomotion.gameObject, enterRoomState, false);
}
if (!string.IsNullOrEmpty(leaveRoomState))
{
Debug.Log("G2");
StateManager.SetState(characterLocomotion.gameObject, leaveRoomState, true);
}
}


}
}
}
 
Please use [ code ] tags when pasting code so it's easier to read. With that said, due to my workload I'm not able to debug custom code. If you can reproduce the issue in the demo scene I can definitely take a look but if not you could start by setting a breakpoint to ensure the correct gravity direction is being returned.
 
Thx for reply , I just solved it ,all of these problem caused but I went from A room with gravity G and B room with gravity -G,the vector.SmoothDamp().normalized make the characterlocmotion.up always the same,so I solved it by just add new Vector3(0,0,0.001) when I enter a oppsite gravity room.
Please use [ code ] tags when pasting code so it's easier to read. With that said, due to my workload I'm not able to debug custom code. If you can reproduce the issue in the demo scene I can definitely take a look but if not you could start by setting a breakpoint to ensure the correct gravity direction is being returned.
 
Back
Top