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
0
Question by SomeRandomGuy · May 31, 2012 at 01:14 PM · rigidbodyaddforce

Little Question regarding rigidbody gravity and addforce.

Hi,

I'm having some problems with AddForce not working after I set the use gravity of my rigidbody to false.

So my question is: Does Addforce work when gravity is turned off?

Thanks in advance!

~Nick

EDIT:

Hmm I think I already figured out what went wrong, still seems weird tho, whenever I change the dodge keys in my script it stops working. the default A and D keys work fine. it seems like it has something to do with my character already moving when the "dodge" is initiated, as A and D are also used to move left and right normally. When changed to Q and E it stops working. Here's a bit of code:

 #pragma strict
 var dodgeKeyRight: KeyCode = KeyCode.D;
 var dodgeKeyLeft: KeyCode = KeyCode.A;
 var lastTime: float = -1.0f;
 var player: GameObject;
 var speed: float = 100;
 var curHor: float;
 var curVert: float;
 var curJump: float;
 var hitGround: boolean = false;
 var baseSpeed: Vector3;
 var baseSideways: Vector3;
 var maxSpeed: float = 2;
 var shootScript: Shooter;
 
 function Awake()
 {
     player = GameObject.FindWithTag("Player");
     shootScript = player.GetComponent(Shooter);
 }
 
 function FixedUpdate()
 {
     CheckGround();
     if(CheckSpeed())
     {
         Movement();
         Dodge();
     }
     if(shootScript.pullIt == true)
     {
         player.rigidbody.useGravity = false;    
     }
     else if(shootScript.pullIt == false)
     {
         player.rigidbody.useGravity = true;
     }
 }
 function Dodge()
 {
     if (Input.GetKeyDown(dodgeKeyRight))
     {
         if (Time.time - lastTime < 0.3f)
         {
             lastTime = -1.0;
             player.rigidbody.AddForce(transform.right*speed*5);
             player.rigidbody.AddForce(transform.up*speed*3);
         } 
         else
         {
             lastTime = Time.time;
         }
     }
     if (Input.GetKeyDown(dodgeKeyLeft))
     {
         if (Time.time - lastTime < 0.3f)
         {
             lastTime = -1.0;
             player.rigidbody.AddForce(-transform.right*speed*5);
             player.rigidbody.AddForce(transform.up*speed*3);
         } 
         else
         {
             lastTime = Time.time;
         }
     }
 
 }
 function Movement()
 {
     curHor = Input.GetAxis("Horizontal");
     curVert = Input.GetAxis("Vertical");
     curJump = Input.GetAxis("Jump");
 
     if(hitGround)
     {
         baseSpeed = transform.forward*curVert*speed/2;
         baseSideways = transform.right*curHor*speed/2;
         player.rigidbody.AddForce(baseSideways);
         player.rigidbody.AddForce(baseSpeed);
         player.rigidbody.AddForce(transform.up*curJump*speed*5);
     }
     if(!Input.GetAxis("Vertical") && !Input.GetAxis("Horizontal") && hitGround)
     {
         player.rigidbody.drag = 100;
     }
     else
     {
         player.rigidbody.drag = 0;
     }
 
     if(Input.GetAxis("Jump"))
     {
         player.rigidbody.drag = 0;
     }
 }
 
 function CheckGround()
 {
     var hit: RaycastHit;
     if (Physics.SphereCast (transform.position, 0.5, -transform.up, hit, .5))
     {
         hitGround = true;
     }
     else if (!Physics.SphereCast (transform.position, 0.5, -transform.up, hit, .5))
     {
         hitGround = false;
     }
 }
 function CheckSpeed():boolean
 {
     if(player.rigidbody.velocity.z < maxSpeed && 
         player.rigidbody.velocity.z > -maxSpeed &&
         player.rigidbody.velocity.x < maxSpeed &&
         player.rigidbody.velocity.x > -maxSpeed)
         {
             return true;
         }
 }
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
1
Best Answer

Answer by whydoidoit · Jun 01, 2012 at 08:13 AM

If you haven't changed the keys in the input manager then rigid body.drag will be set to 100 if the dodge keys don't match the setting for the horizontal axis. Per your Movement function.

Comment
Add comment · Show 2 · 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 whydoidoit · Jun 01, 2012 at 08:14 AM 0
Share

Which of course default to a and d!

avatar image SomeRandomGuy · Jun 01, 2012 at 08:39 AM 0
Share

Oh! Now I get it^^; It seemed so illogical, but it makes sense now, thanks! ^^

avatar image
1

Answer by Wolfram · May 31, 2012 at 01:44 PM

Yes it does, your problem must lie somewhere else. Maybe you could post the part of your script dealilng with forces, and elaborate on "some problems with AddForce"?

Note that with "isKinematic" enabled, forces/physics will be ignored.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

push crate - works in editor, not on device 2 Answers

Rotation problem with spawning bullet 2 Answers

How can I turn on/off Rigidbody.useGravity? 0 Answers

Top Down car/vehicle movement 1 Answer

how get bow to add more force over time 0 Answers


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