- Home /
Maintaining direction while jumping / rotating
Hello,
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.
Any suggestions?
I have a rigidbody attached to the character solely for collision extrapolation. I'm using Translate() for every other aspect of movement aside from the "falling" and "jumping."
Because of the rigidbody I tried using AddForce.
Answer by Lairinus · May 14, 2013 at 04:31 AM
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));
}
}
Specifically here :
jumpDirection = parent.parentObject.transform.TransformDirection(jumpDirection + parent.parentObject.transform.localEulerAngles);
and here :
parent.parentObject.transform.Translate(gravity.ActualGravity(jumpDirection));
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.
Answer by bodec · May 13, 2013 at 04:31 PM
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.
I'll try that.. I already had something like that before but I'll give it another attempt
In short, it still semi-works.
$$anonymous$$y code is :
rigidbody$$anonymous$$anager.SetAndClearDrag(true);
parent.parentObject.rigidbody.AddForce(gravity.ActualGravity(fallingDirection));
the ActualGravity method returns the movement speed and gravity and multiplies it by the vector.
SetAndClearDrag is implemented in order to... set and clear the drag of the rigidbody so that it can move freely in air and be clamped when on the ground. I use is$$anonymous$$inematic as well to reset the force..
One question, though. (I'll post the method as well)
When I jump / fall I only do so in one direction. Despite adding/subtracting the position, it functions the same. (Aka, fallingDirection +/- transform.position)
$$anonymous$$ethod:
public Vector3 ActualGravity(Vector3 movementVector)
{
Vector3 _gravity = movementVector;
_gravity.x *= 5; //$$anonymous$$ultiply by movement speed
_gravity.y = gravity + manageUpwardForce();
_gravity.z *= 5; //$$anonymous$$ultiply by movement speed
return _gravity;
}
Any suggestion on how to have it move in the current direction?
When I get home I'll post a script for you to look at might help
rereading this code on a screen larger than my phone I see the code I was going to toss up wont help as its a rigidbody fps controller is this a game like qubert or are you tossing squares?
I'm using the code as a part of my main character.
I have gravity and movement direction figured out, I just want to jump and rotate while jumping without changing direction
Answer by ABEMOS · Dec 09, 2013 at 05:18 AM
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):
transform.Translate(Vector3.up * Time.deltaTime, Space.World);