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 Calesurf · Mar 10, 2015 at 08:06 PM · rigidbodycapsulecollidermouse lookconstantforce

Rigidbody slowly moves

My player in my game has a rigidbody attatched to him. I use the AddForce function to move him around. I also have a terrain set up. My drag variable of the rigidbody is set to 4 and it has a mass of 1. Gravity is true. The Gravity var moved my player to slowly so I also added a constant force with a -40y force. Everything works great but when I look down or up the player slowly creeps forward when looking down or backwards when looking up. I look around with the mouselook script from the character controllers package. I was wondering is there a way to stop this from happening. The player has a capsule collider also.

Comment
Add comment · Show 3
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 d2 · Mar 10, 2015 at 08:09 PM 0
Share

could you post your code?

avatar image Calesurf · Mar 10, 2015 at 08:12 PM 0
Share

This is the code that makes my Player move

 var water : boolean;
 var water1 : boolean;
 var cam : Transform;
 var water2 : boolean;
 
 
 function Start () {
 
 }
 
 function Update () {
     if(Input.Get$$anonymous$$ey("w") && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && !water1){
         rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 50);
     }
     if(Input.Get$$anonymous$$ey("w") && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)&& !water1){
         rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 40);
     }
     if(Input.Get$$anonymous$$eyDown("space") && !Grounded && !water2){
         rigidbody.AddForce(Vector3.up * 1300);
     }
     if(Input.Get$$anonymous$$ey("a")&& !water1){
         rigidbody.AddForce(transform.right * -30);
     }
     if(Input.Get$$anonymous$$ey("d")&& !water1){
         rigidbody.AddForce(transform.right * 30);
     }
     if(Input.Get$$anonymous$$ey("s")&& !water1){
         rigidbody.AddForce(transform.forward * -30);
     }
     //water movement
     if(Input.Get$$anonymous$$ey("w") && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && water1){
         rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 30);
     }
     if(Input.Get$$anonymous$$ey("w") && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)&& water1){
         rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 20);
     }
     if(Input.Get$$anonymous$$ey("space") && water2){
         rigidbody.AddForce(Vector3.up * 60);
     }
     if(Input.Get$$anonymous$$ey("a")&& water1){
         rigidbody.AddForce(transform.right * -20);
     }
     if(Input.Get$$anonymous$$ey("d")&& water1){
         rigidbody.AddForce(transform.right * 20);
     }
     if(Input.Get$$anonymous$$ey("s")&& water1){
         rigidbody.AddForce(transform.forward * -20);
     }
     
     //Water
     if(cam.transform.position.y < 0){
         water = true;
     }
     if(cam.transform.position.y > 0){
         water = false;
     }
     if(transform.position.y <= 0){
         water1 = true;
     }    
     if(transform.position.y >= 0){
         water1 = false;
     }
     if(transform.position.y >= -.5){
         water2 = false;
     }
     if(transform.position.y <= -.5){
         water2 = true;
     }
     
 }
 
 function OnCollisionEnter(other : Collision){
     Grounded = false;
     if(other.gameObject.tag == "Raft"){
         transform.position.y = 1;
     }
 }
 function OnCollisionExit(other : Collision){
     Grounded = true;
 }
avatar image Calesurf · Mar 10, 2015 at 08:22 PM 0
Share

I have found out it has something to do with the collider. I change it to a sphere collider ins$$anonymous$$d of a capsule collider and it does it less. It stops doing it on flat terrain but still happens on a slope.

1 Reply

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

Answer by Baste · Mar 11, 2015 at 11:12 PM

Don't add force in the direction of transform.forward. If you're looking straight down, transform.forward.x will be very close to 0, so doing this:

 rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 50);

will give you a much lesser speed when looking down than when looking straight ahead.

The easiest thing to do is to remove the y-component from your forward-vector, and then normalize it. That will give you a vector that has constant length, no matter what the tilt of your look is:

 Vector3 myForward = transform.forward;
 myForward.y = 0f;
 myForward = myForward.normalized;

 if(Input.GetKey("w") && Input.GetKey(KeyCode.LeftShift) && !water1){
      rigidbody.AddForce(new Vector3(myForward.x, 0 , myForward.z) * 50);
 }

Do the same thing for transform.right.

I'd also suggest trying to reduce the immense amount of if/elses. Instead of checking left shift and water in every one of those, create a speed variable based on shift and water, and use that for what you multiply your speed with.

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 Calesurf · Mar 12, 2015 at 02:06 AM 0
Share

Thank you, I changed it and it worked perfectly.

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

23 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

Related Questions

OnCollisionEnter not entered on obvious collision 2 Answers

Character controller with Rigidbody to apply force 0 Answers

rigidbody to my player and a box collider to my background not working 0 Answers

Making a surface have accurate friction 0 Answers

My Player With a Capsule Collider is Tipping Over and Rolling After I Added a Rigidbody 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