- Home /
Jumping with force in forward direction
Please understand my problem. I have two things 1) What I want to do is: a) at the press of Jump button I want my character to jump with some length in forward direction. How do I apply force to my jump if no other movement is possible? That is to say my character can only jump. Use of Input Axis lets my character move/skid which I do not want. I only want it to hop and move in hop lengths.
2) I also want to give directions to the jump. But whenever I do it using Transform.Rotate it does not give the desired results.
Please look into the code. I am stuck. #pragma strict var upForce=50; var forwardForce=50; var rotateSpeed=10; function Update () {
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
if (Input.GetButtonDown("Jump")){ rigidbody.AddForce(Vector3.up*upForce); if (transform.position.y>2.1){ rigidbody.AddForce(Vector3.forward*forwardForce); } } }
Answer by SinisterRainbow · Jul 01, 2013 at 05:40 AM
What is your transform.position.y supposed to indicate? you are getting world.y coordinate and I don't think you want to.. If you are trying to find if the user is in the air, it would be better to use a var to record if the player has jumped or not. Then, if he's jumped, apply a force in the transformdirection of the player, using TransformDirection() function. (or rather, apply a (player)forward and (world)up force if jump is occurring). As is, if the player has jumped, he can keep jumping while in the air.
Hey Sinister!! I made changes to the code and I have to say its very close to what I wanted! Thanks a ton!!!
Here is the modified code. Feel free to suggest, dude.
pragma strict
ar upForce=50; var fwdForce=50; var rotateSpeed=10;
function Update () {
var cam : Transform = Camera.main.transform; //var cameraRelativeRight : Vector3 = cam.TransformDirection (Vector3.right); var cameraRelativeUp : Vector3 = cam.TransformDirection (Vector3.up);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var playerPos : Transform = gameObject.transform; var playerPosRight : Vector3 = playerPos.TransformDirection (Vector3.forward);
if (Input.GetButtonDown("Jump")){ // Apply a force relative to the camera's y-axis rigidbody.AddForce(cameraRelativeUp * upForce);
// Apply a force relative to the player's x-axis
rigidbody.AddForce (playerPosRight * fwdForce);
}
}
looks better. You can use the world up position for jumping (unless you're doing something funky gravity). playerPosRight is misnamed, it's his forward, not his right.
is there another problem? addforce will add a force, so if the character is already moving, it just adds on top of it, this may throw off you exact jump requirement. You can add his jump and forward together
AddForce( (playerForward*fwdForce) + (Vector3.up*upForce) );
Actually like I mentioned I am making a game where a character can only hop. So no movement is there except for Jump which gives my character force.
But, there is another problem that crept up. I am using a box collider for my character. When I increase the jump force, it lands and tumbles over(like a box).
I tried adding character collider; sphere colliders, etc. All give me strange outputs.
Any idea?
you should post new questions in a new thread. I dunno, i just started with unity and don't use this, but you don't want box collider i'm pretty sure. You should have an animation play when a player lands from a jump rather than let forces dictate (unless he is a ragdoll).. in other engines you usually have special collider for arms/torso/etc that register weapon hits, then a capsule covering most of the body for collision detection. Dunno about unity characters yet.
Answer by Darwin Mecharov · Apr 08, 2014 at 05:22 AM
@Senrohit, if you're object has a rigidbody, try using rigidbody.freezeRotation so that your collider doesn't tumble over. It works for me, only allowing rotation when my mouse is moving in x or y axis.
Thank you, this works for me. I freeze rotation like you suggest and un-freeze when collision happens. Everything works as I want.