Tuesday, June 14, 2022
HomeGame Developmentunity - Why is Vector3.ProjectOnPlane() "drifting" farther from desired rotation on steep...

unity – Why is Vector3.ProjectOnPlane() “drifting” farther from desired rotation on steep slopes


I am writing a easy participant motion script in Unity 3D, and I have been utilizing Vector3.ProjectOnPlane to perform slope/ramp motion. For probably the most half, this works on smaller slopes however I’ve observed that the steeper the slope is, the extra the route appears to float additional away than anticipated, inflicting the participant to stroll additional to 1 aspect than the enter would recommend.

(To make clear, orientationTransform is rotated through the Mouse Enter, thus solely needing to make use of rework.ahead/proper within the motion calculation.)

Code:

// Excerpt from predominant motion loop
Vector3 movementDirection = (orientationTransform.ahead.normalized * inputs.y + orientationTransform.proper.normalized * inputs.x).normalized;

if (stateHandler.IsGrounded) {
  if(stateHandler.IsOnSlope) {
    Debug.DrawRay(orientationTransform.place, movementDirection.normalized * 3f, Colour.magenta);
    return GetVectorOnSlope(movementDirection) * movementForce * Time.fixedDeltaTime;
  } else {
    return movementDirection * movementForce * Time.fixedDeltaTime;
  }
}

// Vector Operate
Vector3 GetVectorOnSlope(Vector3 vector) {
  RaycastHit hitInfo;
  if (Physics.Raycast(orientationTransform.rework.place, Vector3.down, out hitInfo, 
  1f, groundLayer)) {
    Debug.DrawRay(orientationTransform.place, Vector3.ProjectOnPlane(vector, 
    hitInfo.regular) * 2f, Colour.crimson);
    return Vector3.ProjectOnPlane(vector, hitInfo.regular).normalized;
  } else {
    return vector;
  }
}

Instance:
Desired Course – Magenta
ProjectOnPlane Course – Purple
Example of movement "skewing"

I’ve tried normalizing the end result and experimenting with Vector3.Cross in addition to trying to skip utilizing Vector3.ProjectOnPlane altogether, however I’ve not had a lot luck.

Is that this the right method for me to be implementing this? I really feel like Vector3.ProjectOnPlane is behaving precisely because it ought to, however I can not determine an alternative choice to obtain what I am searching for.

Any assistance is drastically appreciated!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments