I have three Vector3s: movementVector, jumpVector, fallVector.
I want to be able to jump forward and rotate my object without its direction changing. I just want to make a 180 and still head in the direction without always respecting the localEulerAngles. (My camera script changes my object’s look angle to match the Camera’s)
movementVector converts input to the object’s local euler angles. It works fine.
jumpVector is set to movementVector at the time of jumping. This value never changes until a new jump is made.
fallVector is set the second the object isn’t grounded / the jump ends.
AddForce will not work (at least not with my methodology), as I have incremental Gravity and if I AddForce more than once (to update the gravity) I get thrown outside of the map. At that, I tried some things with Lerp with low success mainly because it also doesn’t seem to respect the gravity.
if you set up your forward movement in a statement of if grounded that should solve your being able to add force while in the air stopping you from moving forward or backward.
12 hours later and I found a solution using transform.Translate().
My full code is :
private bool towardAerialAngles = false;
void translateUnit()
{
//Figure out how to jump in one direction and continue going in one direction
if (gravity.isGrounded && !gravity.isJumping) //If the unit is grounded
{
Ray ray = new Ray(transform.position,transform.forward); //Make a ray shoot foward **Change Eventually**
RaycastHit hit;
if (!Physics.Raycast (ray, out hit, _groundedRayDistance)) //Cast the ray in the movement direction
{
towardAerialAngles = false;
_gravityDisplacement = _movementVector;
parent.parentObject.transform.Translate(gravity.ActualGravity(_movementVector) * 2); //Move if there wasn't a hit
}
}
else if (!gravity.isGrounded)
{
if (gravity.isJumping)
{
if (!towardAerialAngles)
{
towardAerialAngles = true;
jumpDirection = parent.parentObject.transform.TransformDirection(jumpDirection + parent.parentObject.transform.localEulerAngles);
}
parent.parentObject.transform.Translate(gravity.ActualGravity(jumpDirection), Space.World);
}
else
{
if (!towardAerialAngles)
{
towardAerialAngles = true;
jumpDirection = parent.parentObject.transform.TransformDirection(jumpDirection + parent.parentObject.transform.localEulerAngles);
}
parent.parentObject.transform.Translate(gravity.ActualGravity(jumpDirection), Space.World);
}
}
else
{
towardAerialAngles = false;
parent.parentObject.transform.Translate(gravity.ActualGravity(_movementVector));
}
}
Same concept as stopping an IEnumerator with the boolean, except I call it once when the unit isn’t grounded. Then when the unit is grounded, I set it to true.
jumpDirection is set in a different script the second the unit isn’t grounded.
Hope this helps out anyone with similar answers… sure spent long enough on it lol.
Also, ugly as hell and not commented at this point, too excited.
I am relatively new to unity, but have you tried adding " Space.World" to your transform.Translate? this should make the movement continue to happen in the absolute set direction regardless of the new rotation of the object.
e.g. (from the scripting reference):