Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Deridealized · Jan 16, 2017 at 01:13 PM · physicsrigidbodyplayer movementfalling

Rigidbody falling not according to physics, only when script is applied.

Hi,

I've read about this topic for the past 3 hours in an attempt to fix it without asking for help, but I just can't find a solution.

The physics in my scene work perfectly for the primitives I have in there, except for my player. The second I add the playerController script to any asset with a Rigidbody on it they stop being affected by physics the same way.

I've tried upping the gravity, changing the scale of the object, and increasing forces artificially (though my current novice skills fail me). Yet here I am still dumbfounded.

If I import a cube, it falls perfectly fine. I attach my script (basic left, right, sprint and jump functions) and it suddenly acts really light and floaty.

Can anybody help me please?

{

 public float walkSpeed;
 public float runSpeed;

 //JUMP VARS
 bool playerGrounded = false; //Is the player on the ground?
 Collider[] groundCollisions;  //??
 float groundCheckRadius = 0.2f; //
 public LayerMask groundLayer;   //Set in Unity, Layer for ground
 public Transform groundCheck;   //Set in Unity, OBJECT for ground detection
 public float jumpHeight;        //jump height
 bool running;

 Rigidbody playerRB;

 // Use this for initialization
 void Start()
 {
     playerRB = GetComponent<Rigidbody>();
 }   
 
 void FixedUpdate () {
     
     //Movement left and right
     running = false;

     float move = Input.GetAxis("Horizontal");           //Assigns A and D to move +1 or -1

     float sprint = Input.GetAxis("Fire3");              //Shift
             

     if ((move > 0 || move < 0) && (sprint > 0 && playerGrounded)) //If A or D && Shift && Grounded
     {
         
         playerRB.velocity = new Vector3(move * runSpeed, 0, 0);     //This one triggers if moving and holding shift whilst moving
     }
     else
     {
         playerRB.velocity = new Vector3(move * walkSpeed, 0, 0);    //This one allows movement
     }
     //----
    

     //JUMPING

     //Set the ground check collider
     groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);

     //Is the player on the ground?
     if (groundCollisions.Length > 0) //Array.Length - A collision will add 1 or more to the array
     {
         playerGrounded = true;
     }
     else
     {
         playerGrounded = false;
     }

     if (playerGrounded && Input.GetAxis("Jump") > 0)
     {
         playerGrounded = false;
         playerRB.velocity = new Vector3(playerRB.velocity.x, 0, 0); //Sets the Y velocity (up/down) to 0 for consistency
         playerRB.AddForce(0, jumpHeight, 0, ForceMode.Impulse);
     }
    

  } //end of fixed update

}

Any help you can provide will be greatly appreciated, thanks for your time, and I apologise if this question has already been answered, it's now 3am and I've been sat here trying not to pull my own eyes out since midnight. Thanks again.

Comment
Add comment · Show 2
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 doublemax · Jan 16, 2017 at 01:15 PM 1
Share
 playerRB.velocity = new Vector3(move * runSpeed, 0, 0); 

If you set the velocity directly, you're overriding any velocity that might be caused by other forces, e.g. gravity. Use AddForce() ins$$anonymous$$d, which will then be added to all other forces.

avatar image Deridealized doublemax · Jan 19, 2017 at 04:04 PM 0
Share

That worked, thanks! However I have used this before and it works..

{ if ((sneaking > 0 || firing > 0) && playerGrounded)
{
playerRb.velocity = new Vector3(move walkSpeed, playerRb.velocity.y, 0);
} else
{ playerRb.velocity = new Vector3(move
runSpeed, playerRb.velocity.y, 0);
if ($$anonymous$$athf.Abs(move) > 0) running = true; } }

and this is affected by gravity fine.

0 Replies

· Add your reply
  • Sort: 

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

130 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody can push but can't be pushed 2 Answers

Player moves indefinitely after collision 0 Answers

Player Movement Goes Crazy When Hit On Side Of Moving Platform 0 Answers

Ball Speed is not increasing as per code 0 Answers

Making a rigidbody "immune" to reacting with another specific rigidbody? 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