- Home /
Question by
WalidMohamed · Aug 13, 2016 at 05:42 PM ·
unity 5objectjumpbounce
how to make give a cube the capability to make an object jump when it comes on it ??
i wanna make the player which is a ball jump when it touches particular cubes ,but not using bouncing because it doesn't give the same value of jump every time and thanks
Comment
Answer by BloodBTF · Aug 13, 2016 at 07:03 PM
You could try
1.making a custom tag for the cube that allows you to jump and call it something like "Jumpable"
2.make a script, or edit your player script and add this.
public float jumpPower = 5f;
bool hit;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
//or just assign the rigidbody yourself
}
void OnCollisionEnter(Collision col)
{
if (col.transform.tag == "Jumpable")
hit = true;
}
void OnCollisionExit(Collision col)
{
if (col.transform.tag == "Jumpable")
hit = false;
}
void Update()
{
if (hit)
{
rb.AddForce(0f,jumpPower,0f,ForceMode.Accelaration);
}
}
THEN:
Assign this script to your player's COLLIDER, not the OBJECT ITSELF. be sure to assign the players rigidbody in the inspector to the script
Does the object that needs to jump an rigidbody AND does the object it needs to touch have a collider?
yes Look i want to make the ball jumps when it hits the cube but that's by the cube power not it's