Question by
FyRE_FREE · Aug 13, 2020 at 01:54 AM ·
collisionrigidbodyaddforceoncollisionstay
How to check fo rigidbody collision?
I am trying to make a boost/bounce pad script, but right now it only supports the player, and not any props. all of my props have rigidbodies, prop tags and prop layers. I have tried everything i could. please, what updates do I have to make to make it support props? When a prop touches the pad it should launch in the xyzForce or xyzVelocity direction.
Heres the code!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Booster : MonoBehaviour
{
// Object References
public Rigidbody playerRB;
public GameObject boostPad;
// Vector Values
public int xForce = 0;
public int yForce = 0;
public int zForce = 0;
public int xVelocity = -0;
public int yVelocity = -0;
public int zVelocity = -0;
// Start is called before the first frame update
void OnCollisionStay(Collision collisionInfo)
{
if(collisionInfo.collider.tag == "Player")
{
if (xForce != 0)
playerRB.AddForce(xForce, 0, 0);
if (yForce != 0)
playerRB.AddForce(0, yForce, 0);
if (zForce != 0)
playerRB.AddForce(0, 0, zForce);
// this applies velocity
if (xVelocity != 0)
playerRB.velocity = new Vector3(xVelocity, 0, 0);
if (yVelocity != 0)
playerRB.velocity = new Vector3(0, yVelocity, 0);
if (zVelocity != 0)
playerRB.velocity = new Vector3(0, 0, zVelocity);
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Comment
Your answer
Follow this Question
Related Questions
Apply Force when a Rigidbody ExitCollider 2 Answers
Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers
rigidbody.Addforce stacking? 2 Answers
Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer
OnCollisionEnter returning incorrect collision points 0 Answers