Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by senrohit · Jul 01, 2013 at 05:30 AM · jumpdirectionaxisforce

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); } } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image senrohit · Jul 01, 2013 at 07:51 AM 0
Share

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);

 }

}

avatar image SinisterRainbow · Jul 01, 2013 at 08:44 AM 1
Share

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) );
avatar image senrohit · Jul 01, 2013 at 09:14 AM 0
Share

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?

avatar image SinisterRainbow · Jul 01, 2013 at 10:23 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WarBuggy · Mar 18, 2015 at 12:20 PM 0
Share

Thank you, this works for me. I freeze rotation like you suggest and un-freeze when collision happens. Everything works as I want.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy AI Movement one axis at a time (8 directional - 2.5d) towards player/target 2 Answers

How to get the direction of the velocity of an object (c#) 1 Answer

How do I lock Y Axis movement for Parented Camera? 3 Answers

Convert Force into Velocity for 2D Player Jump 0 Answers

Inhibit force from doubling when 2 axes are active 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges