- Home /
AddForce problem
Hi there! I'm using AddForce to make a character jump. But when I'm hitting Space very often - he flies like a rocket. How to fix that? Thank you!
void Update ()
{
horizontal = Input.GetAxis("Horizontal");
if (Input.GetKeyDown (KeyCode.Space) && can_jump == 1)
{
if (horizontal == 0)
{
animator.SetInteger("jump", 1);
gameObject.GetComponent<Rigidbody>().AddForce(0,200,0);
}
else
{
if (horizontal > 0)
{
animator.SetInteger("jump_ahead", 1);
gameObject.GetComponent<Rigidbody>().AddForce(0,200,100);
}
if (horizontal < 0)
{
animator.SetInteger("jump_ahead", 1);
gameObject.GetComponent<Rigidbody>().AddForce(0,200,-100);
}
}
}
if (Input.GetKeyUp (KeyCode.Space))
{
animator.SetInteger("jump", 0);
animator.SetInteger("jump_ahead", 0);
}
}
What behaviour are you ai$$anonymous$$g for?
If for example, you want a naturalistic system where the character can only jump when it's standing on something it can push against, you could check for that before applying the force.
Alternatively, if you want to be able to increase the jump height while in the air by hitting space again (but less dramatically), you could try zeroing the rigidbody's velocity (or just its upward component) before applying the force.
I want to know why does a character speeds up more then before and how to fix that.
He speeds up because you keep adding a force. You fix it by not doing that.
Beyond that.... it depends on the answer to my question.
By watching your video, i don't see a problem. It does everything you asked it to do.
Answer by HarshadK · Jun 03, 2015 at 01:21 PM
Allow your player to only jump if he is grounded.
Set a isGrounded boolean that you check for being true before adding jump force to the player. Once the player jumps set this boolean to be false and again set it to true when player lands on the ground to allow player to jump again. You can check the collision of player with ground to set this boolean to be true.
I'm already doing this.
using UnityEngine;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
float horizontal = 0;
Animator animator;
int can_jump = 0;
// Use this for initialization
void Start ()
{
animator = gameObject.GetComponent<Animator>();
//print(gameObject.transform.rotation);
}
void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "ground")
{
can_jump = 1;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.transform.tag == "ground")
{
can_jump = 0;
}
}
// Update is called once per frame
void Update ()
{
horizontal = Input.GetAxis("Horizontal");
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && can_jump == 1)
{
if (horizontal == 0)
{
animator.SetInteger("jump", 1);
gameObject.GetComponent<Rigidbody>().AddForce(0,200,0);
}
else
{
if (horizontal > 0)
{
animator.SetInteger("jump_ahead", 1);
gameObject.GetComponent<Rigidbody>().AddForce(0,200,100);
}
if (horizontal < 0)
{
animator.SetInteger("jump_ahead", 1);
gameObject.GetComponent<Rigidbody>().AddForce(0,200,-100);
}
}
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space))
{
animator.SetInteger("jump", 0);
animator.SetInteger("jump_ahead", 0);
}
}
Vector3 temp1;
Quaternion temp2;
void FixedUpdate()
{
animator.SetFloat("walking_f", horizontal);
animator.SetFloat("walking_b", horizontal);
if (horizontal >= 0.1)
{
temp2 = transform.rotation;
temp2.y = 0;
transform.rotation = temp2;
temp1 = transform.position;
temp1.z += 3.5f* Time.deltaTime;
transform.position = temp1;
}
if (horizontal < 0)
{
temp2 = transform.rotation;
temp2.y = 180;
transform.rotation = temp2;
temp1 = transform.position;
temp1.z -= 3.5f* Time.deltaTime;
transform.position = temp1;
}
}
}
Rather than setting the can_jump from OnCollisionExit try to set it once the player makes the jump. Also it is better to use a boolean for can_jump rather than an int.