- Home /
My sphere loses velocity when jumping
Hello, I am using a rigidbody sphere to move around and when ever I try to jump my object loses all velocity and it's jumps cover as much distance as when it is completely stopped. could you help me find a solution? Im new to scripting so please explain how the solution works ;-;
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Jump : MonoBehaviour { public bool OnGround; private Rigidbody rb; public float jumpspeed = 10;
private List<string> tagsToCompare = new List<string>()
{
"Ground",
"PickUp",
"tag3"
};
void Start()
{
OnGround = true;
Debug.Log("OnGround set to true in Start");
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (OnGround)
{
if (Input.GetButton("Jump"))
{
rb.velocity = new Vector3(0.0f, jumpspeed, 0.0f);
OnGround = false;
Debug.Log("OnGround set to false in Update");
}
else
{
}
}
}
void OnCollisionEnter(Collision other)
{
Debug.Log("Collision entered with tag named: " + other.gameObject.tag);
if (isTargetTag(other.gameObject.tag)) // NEW
{
OnGround = true;
Debug.Log("OnGround Set To true in OnCollisionEnter, collided with tag: " + other.gameObject.tag);
}
else
{
if (other.gameObject.CompareTag("Wall"))
{
}
else
{
OnGround = false;
}
}
}
private bool isTargetTag(string tagToCheck)
{
return tagsToCompare.Contains(tagToCheck);
}
}
I can't seem to replace my rb.velocity = new velocity with addforce or the other ones... could you please show me a solution .-.
Answer by UnityCoach · Dec 08, 2016 at 02:35 AM
use
rb.AddForce (new Vector3(0.0f, jumpspeed, 0.0f));
Thx for telling me how to implement it :) . but you don't need to -1 my rep. It takes 2x as long to fix code when i have to wait for mod to verify my post ;-;
The -1 is propably because "I can't seem to replace my rb.velocity = ..." is not a very good answer. You should post comments as comments. I'll try to sort this one out...the answer is the comment to this non-answer...argh.
Answer by UnityCoach · Dec 07, 2016 at 04:32 PM
Try using AddForce () or AddRelativeForce () instead of assigning the rigid body velocity.
Your answer
Follow this Question
Related Questions
RigidBody immediately stops after AddForce 1 Answer
Fireing a tank projectile. 1 Answer
Unity Physics On Input Issue? 0 Answers
How do I add a pulling force? 1 Answer