Hi there, I was previously working in version 2.1.1 and made a simple abilty that inherited from AlignToGravity.
The ability basically changed the gravity of the character to align to the normal of a surface and caused the player character to fall and rotate to that surface (so the characters local 'up' matched the normal of the surface he's standing on, effectively making it the new 'floor'.
The problem is after upgrading to 2.1.8, the player still falls but no longer rotates to the normal of the surface.
Here is the rotation code from 2.1.1 within AlignToGravity:
Looking through the release notes, a fix was made in 2.1.2 which Fixed AlignToGravityZone from not correctly detecting if it can stop.
I'm assuming that this caused the change in the rotate function which is currently used in 2.1.8 (in addition to other changes). Here is the updated AlignToGravity code in 2.1.8
And here was the ability I used to achieve the change in gravity and rotation of the character towards the normal of the surface I wanted him to rotate to:
Additional, there are also other differences between AlignToGravity 2.11 and 2.1.8. AbilityStarted() has changed, there's an Update() in the 2.1.1 version but only a late update in 2.1.8 etc
I wonder if you could give me a pointer as to why the character may not rotate as he used to and a potential fix?
Thanks so much for any assistance.
The ability basically changed the gravity of the character to align to the normal of a surface and caused the player character to fall and rotate to that surface (so the characters local 'up' matched the normal of the surface he's standing on, effectively making it the new 'floor'.
The problem is after upgrading to 2.1.8, the player still falls but no longer rotates to the normal of the surface.
Here is the rotation code from 2.1.1 within AlignToGravity:
Code:
/// <summary>
/// Rotates the character to be oriented with the specified normal.
/// </summary>
/// <param name="targetNormal">The direction that the character should be oriented towards on the vertical axis.</param>
protected void Rotate(Vector3 targetNormal)
{
var rotation = m_Transform.rotation * m_CharacterLocomotion.Torque;
var proj = (rotation * Vector3.forward) - (Vector3.Dot((rotation * Vector3.forward), targetNormal)) * targetNormal;
if (proj.sqrMagnitude > 0.0001f) {
var alignToGroundSpeed = m_CharacterLocomotion.Platform == null ? m_RotationSpeed * m_CharacterLocomotion.TimeScale * TimeUtility.FixedDeltaTimeScaled : 1;
var targetRotation = Quaternion.Slerp(rotation, Quaternion.LookRotation(proj, targetNormal), alignToGroundSpeed);
var rotationDelta = m_CharacterLocomotion.Torque * (Quaternion.Inverse(rotation) * targetRotation);
var collisionRotationDelta = m_CharacterLocomotion.CheckRotation(rotationDelta, true);
// If the collision rotation is the same as the rotation delta then there are no collisions with aligning to the ground and the maximum
// rotation delta should be applied (the collision rotation delta). If, however, there is a collision then only the original rotation delta
// should be applied so the character can still rotate from input/root motion.
m_CharacterLocomotion.Torque = (collisionRotationDelta == rotationDelta ? collisionRotationDelta : m_CharacterLocomotion.Torque);
}
}
Looking through the release notes, a fix was made in 2.1.2 which Fixed AlignToGravityZone from not correctly detecting if it can stop.
I'm assuming that this caused the change in the rotate function which is currently used in 2.1.8 (in addition to other changes). Here is the updated AlignToGravity code in 2.1.8
Code:
/// <summary>
/// Rotates the character to be oriented with the specified normal.
/// </summary>
/// <param name="targetNormal">The direction that the character should be oriented towards on the vertical axis.</param>
protected void Rotate(Vector3 targetNormal)
{
var deltaRotation = Quaternion.Euler(m_CharacterLocomotion.DeltaRotation);
var rotation = m_Transform.rotation * deltaRotation;
var proj = (rotation * Vector3.forward) - (Vector3.Dot((rotation * Vector3.forward), targetNormal)) * targetNormal;
if (proj.sqrMagnitude > 0.0001f) {
Quaternion targetRotation;
if (m_CharacterLocomotion.Platform == null && !m_Stopping) {
var alignToGroundSpeed = m_RotationSpeed * m_CharacterLocomotion.TimeScale * Time.timeScale * m_CharacterLocomotion.DeltaTime;
targetRotation = Quaternion.Slerp(rotation, Quaternion.LookRotation(proj, targetNormal), alignToGroundSpeed);
} else {
targetRotation = Quaternion.LookRotation(proj, targetNormal);
}
var rotationDelta = deltaRotation * (Quaternion.Inverse(rotation) * targetRotation);
m_CharacterLocomotion.DeltaRotation = rotationDelta.eulerAngles;
}
}
And here was the ability I used to achieve the change in gravity and rotation of the character towards the normal of the surface I wanted him to rotate to:
Code:
using UnityEngine;
using Opsive.UltimateCharacterController.Character.Abilities;
using Opsive.UltimateCharacterController.Events;
using Opsive.UltimateCharacterController.Character;
public class AlignToGravityNormal : AlignToGravity
{
private Vector3 m_targetNormal = Vector3.up;
public override void Awake()
{
base.Awake();
EventHandler.RegisterEvent<Vector3>( "ChangeGravity", UpdateTargetNormal);
}
private void UpdateTargetNormal(Vector3 newTargetNormal )
{
var characterLocomotion = GetComponent<UltimateCharacterLocomotion>();
if ( characterLocomotion!=null )
{
characterLocomotion.GravityDirection = -newTargetNormal.normalized;
//Make the player auto jump before fallign and rotating
AddForce(m_CharacterLocomotion.Up * 0.1f, 1, false, false);
}
m_targetNormal = newTargetNormal.normalized;
}
public override void OnDestroy()
{
base.OnDestroy();
EventHandler.UnregisterEvent<Vector3>("ChangeGravity", UpdateTargetNormal);
}
public override void ApplyRotation()
{
Rotate(m_targetNormal);
}
}
Additional, there are also other differences between AlignToGravity 2.11 and 2.1.8. AbilityStarted() has changed, there's an Update() in the 2.1.1 version but only a late update in 2.1.8 etc
I wonder if you could give me a pointer as to why the character may not rotate as he used to and a potential fix?
Thanks so much for any assistance.