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 Gumpert · Jan 13, 2015 at 10:20 PM · 2d-platformer2d-physicsmathf.clampplatformercontroller

Mathf.Clamp for rigidbody2D.velocity.x not working?

Hi everyone!

So I've been making a 2D game, using the 2D physics engine and stuff. It's a platformer. My player character is a little rigidbody person, with a script i made attatched that controls movement and animation. So, what I was trying to get it to do is make it so that when the character is on the ground, the acceleration is almost instant, but when the character is in freefall, there is still some control over horizontal speed, but the acceleration is slow.

Now, I've managed to make my script do this fine, but there was a slight problem with this - if the player runs at full speed, then jumps, and then keeps holding the key to run, they would accelerate beyond their normal ground running speed, and this does look a bit rubbish, so what i'm trying to do is clamp the velocity in the x axis.

So I've added in this line right at the end of my Function FixedUpdate:

 Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);

forwardSpeed is a float that represents the maximum speed at which the character runs when grounded.

I was hoping that this would mean that the x velocity would never exceed the normal running speed, but this line appears to have no affect whatsoever.

So, i'll post some more of the script - i won't give you all of it - all the animation control and stuff is in there too, i'll just keep what's relevant so you don't waste too much time trawling through all of it trying to find the relevant lines. I can tell you now that the problem is most likely to be in the FixedUpdate function, near the end.

 //So, i'll explain what these variables do later on in the script, but i'll just tell you what their values are here.
 //It shouldn't matter what their values are, but i might as well tell you anyway.
 var forwardSpeed : float;  //5
 var airSpeed : float;    //Ok, so this is actually a force. Yeah, i know, it shouldn't be called speed... i was using it for somehting
 //different, i havent got around to renaming it yet... this one would be about 10 (the rigidbody's mass is 1, so that would create an
 //acceleration of 10ms^-2), but i've set it to like 1000 for testing purposes at the moment
 var jumpTime : float;
 
 private var lastCollision : float;
 private var grounded : boolean;
 private var jumpSpeed : float;
 private var collisionInfo : Collision2D;
 
 
 function Start(){
 //So this line here is simply because I find it more convenient to decide on a duration time for a jump rather than a starting speed.
 jumpSpeed = jumpTime * -1*Physics2D.gravity.y * 0.5;
 }
 
 
 
 
 function OnCollisionStay2D(collisionInfo){
 //This is my way of checking whether the character is grounded or not - basically, if the character's collider is in contact with
 //anything, it checks the normal of that contact point or contact points. If it's/they're pointing roughly upwards, the character is
 //grounded. The lastCollision bit i'm using because there isn't any functionOnCollisionStayOutOf if you get what I mean. I use that
 //variable to set grounded to false if necessary. It'll make more sense later, there's more stuff about it in the fixedUpdate function.
 for(var i : int = 0; i < collisionInfo.contacts.length; i++){
 if (collisionInfo.contacts[i].normal.y>0.9){
 grounded = true;
 lastCollision = Time.time;
 }
 }
 }
 
 
 
 
 function FixedUpdate(){
 //Ok, this is the bit i was talking about - basically, if we didn't get grounded set to true this physics step, grounded is set to false.
 //This stuff all works so far by the way.
 if (Time.time - lastCollision > 0.02){
 grounded = false;
 }
 
 //Ok - this should be fairly self explanatory - if we're grounded, the speed is equal to the... uh... yeah...
 if (grounded){
 rigidbody2D.velocity.x = Input.GetAxis("Horizontal") * forwardSpeed;
 }
 
 //ah - ok, we're finally here - the horizontal velocity control while in freefall - instead of modifying the velocity directly here, i'm
 //using AddForce to give it a gradual acceleration. This does all work still.
 else {
 rigidbody2D.AddForce(airSpeed*Vector2(Input.GetAxis("Horizontal"), 0.0));
 }
 
 //Here's jumping.
 if (grounded && Input.GetButton("Jump")){
 rigidbody2D.velocity.y = jumpSpeed;
 }
 
 //Ok, this is the bit thats not working. As you can see, this is the final line of the FixedUpdate function, so it should happen after
 //everything else. The idea behind this line is to clamp the x velocity such that |v|=<forwardSpeed. But this doesn't have any effect
 //at all on what actually happens. I don' get any error messages or anything, it just doesn't work.
 Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);
 }
 
 
 
 function Update(){
 //oh yeah, i just put this in to make sure i wasn't imagining stuff. And yes, i was getting completely ridiculous speeds, way way above forwardSpeed.
 Debug.Log(rigidbody2D.velocity.x.ToString());
 }


So yeah, I really have no idea what's going on here - any help would be very much appreciated. Sorry about the messy code, as you can probably see, it's a bit longwinded and inefficient, i am quite new to coding and unity. Thanks in advance for any help!

Blessings of the nine divines upon you,

James

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skylem · Jan 13, 2015 at 10:29 PM

just about how u've declared u nearly had it actually all u need to do is tell it what value it should be Clamping, hope this helps u .

 // JS
     var f = Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);
     var rigidbody2D.velocity.x = Mathf.Clamp(, -forwardSpeed, forwardSpeed); 
 // i dont use JS anymore so i can't say which of the above u need but atleast 1 should work.
 // c# 
     float f = Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);




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 Gumpert · Jan 13, 2015 at 10:47 PM 0
Share

haha! thanks, that solved it completely!! i can't believe i was stuck on this for so long! thanks so much, i really appreciate this - very helpful. once again, the unity community solves all my problems - amazing.

avatar image skylem · Jan 14, 2015 at 12:40 AM 0
Share

No problem at all, its usually the little things that get most people or atleast myself haha. Please mark the question answered so others know u've solved your issue and if u need help with anything in the future feel free to email me Skylem@live.com.au

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

27 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

Related Questions

C# 2D Jumping with AddForce doesnt work 1 Answer

2D Movement Mechanics for Platformer Game 0 Answers

I have a problem with Layermask Unity2D 0 Answers

How To Make My 2D Character Jump Gradually 2 Answers

[2D] Sprite flickers/is choppy when moving 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