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);
}
}
}
}
}
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);
}
}
}
}
}