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 ethan-smith · Apr 22, 2015 at 11:14 AM · rigidbodycharacterrigidbody physicsinconsistent

Jump works but is not the same height every time i jump

i cant use gravity for starters. im trying to create a rigidbody character controller to walk on walls or rounded surfaces. I can jump but when I do its not the same every time.

also another question... I have to create a rigidbody variable in unity 5 and assign the gameobject the script is attatched to to access the already present rigidbody component attached to my gameobject. is there anyway around this.

ex.`rigidbody.velocity` doesn't show up in mono develop. if I add a rigidbody variable and assign the gameobject the script is attached to to itself then I can access it that way but its just more typing.

ex. public rigidbody character; character.velocity that shows.

anyways back to my original question. sorry. I can ask that later if I need to.

 public Rigidbody TheCharacter; //this is the character
     public float Gravity = 9.8f; //this is gravity 
     public float JumpForce = 10.0f; //this is the force of jumping
     public float DistanceFromGroundMargin = 0.2f; //the player can jump if within this distance from "ground"
     private RaycastHit Hit; //this is the raycast to check if the player is grounded and to find the normal of the polygon underneath the player
     private Vector3 MyNormal = Vector3.zero; //this is the normal of the polygon underneath the player
     private float DistanceFromGround; //this is the distance from the ground when grounded. it equals half the height of the collider minus the position of the collider relative to the orgin of the gameobject the collider is attached to incase the collider is off center from the gameobject
     public CapsuleCollider TheCollider; //this is the collider needed
     private bool IsGrounded; //the boolean to tell whether or not the character is grounded or not (touching ground)
     // Use this for initialization
     void Start () {
         TheCharacter.useGravity = false; //turns rigidbody global gravity off
         TheCharacter.freezeRotation = true; //stops unwanted roatation from rigidbody physics
         MyNormal = TheCharacter.transform.up; //sets the normal equal to the rotation of the players local up 
         DistanceFromGround = TheCollider.bounds.extents.y - TheCollider.center.y; //read the variable above
         Physics.Raycast (transform.position, -transform.up, out Hit); //casts a ray to for the next line to...
         IsGrounded = Hit.distance <= DistanceFromGround + DistanceFromGroundMargin; //check if the player is grounded by comparing the distance from where the ray was cast (gameobject center) to the object if any beneath it. if the distance is less than DistanceFromGround + DistanceFromGroundMargin then the player is grounded
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         TheCharacter.AddForce (-Gravity * TheCharacter.mass * MyNormal); //adds a downward force in the direction of the characters local -up (downward) direction. this local direction shoudl be equal to the normal beneath the character at any given time
         Physics.Raycast (transform.position, -transform.up, out Hit);
         IsGrounded = Hit.distance <= DistanceFromGround + DistanceFromGroundMargin; //checks if the player is grounded... for more info read the check in the start function
         if (Input.GetKey (KeyCode.Space) & IsGrounded) { //if the space bar is pushed and the player is touching ground then...
             TheCharacter.velocity += JumpForce  * MyNormal; // change the characters velocity to an upwards force using the JumpForce variable
     //        TheCharacter.AddForce (JumpForce * TheCharacter.mass * MyNormal);
         }
     }




when I jump it doesn't jump the same amount everytime. any solutions? just smash the space bar after you set up the character.

(to set it up create a capsule and (optional camera that is child to the capsule) add a capsule collider and rigidbody and assign them to the script once the script is attatched to the capsule then pound space and watch in the scene view)

I tried addForce and velocity to see if the problem stopped and I even multiplied by jumpforce and character mass and the normal but still no change (I tried without character mass as well)

thanks for any help. :)

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
Best Answer

Answer by Mapleman · Apr 22, 2015 at 05:27 PM

I think your problem is using the Input.GetKey method. See, it returns true on every frame if the given key is kept pressed. So it might be possible that player is still on the ground when next frame is handled, but there is allready some force applied to it. So you are adding some more upward force to your character.

Try using Input.GetKeyDown instead. That returns true only once when the key is pressed down. Obviously when key is released and pressed again, it again returns true.

About your other question. If I understood correctly, you are wondering how to access the gameobject the script is attached to. Simply put, you get a reference to gameObject via the 'gameObject' property inside your script. Using that gameObject property you can query all other components attached to that gameobject and so on. I usually use it so, that if I need references to some colliders or anything, I introduce some class members (variables) and fetch the components in Start() method.

For example if I had a LineRenderer component in my GameObject, I would write in Start() :

 myLineRenderer = gameObject.GetComponent<LineRenderer>();



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 ethan-smith · Apr 23, 2015 at 03:04 AM 0
Share

youre a genius $$anonymous$$apleman!!!!!! I don't know why I didn't realize that from the start. thank you for the help so very much for answering both questions!!!!! :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How Character controller can push another character controller? 0 Answers

mid air controlling problem with rigidbody 1 Answer

Using Mecanim Animator to move Model Left & Right 0 Answers

RigidBody Character Controller Bug 0 Answers

'height' is not a member of 'UnityEngine.Collider'. 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