- Home /
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. :)
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>();
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!!!!! :)