I’m stuck a bit and need help.
In this game I want the ball to become inactive when it collides with the right-hand platform.
Here’s my script (that’s of course not working).
public class BallController : MonoBehaviour
{
Rigidbody rb;
public float forceY = 150f;
public bool isGrounded;
void Start()
{
rb = this.GetComponent();
isGrounded = false;
}
void Update()
{
if (isGrounded = true && Input.GetMouseButton(0))
{
rb.AddForce(new Vector3(10f, forceY, 0f));
}
}
void onCollisionEnter (Collision col)
{
if (col.gameObject.name == “platformL”)
{
rb = this.GetComponent();
isGrounded = true;
rb.AddForce(new Vector3(10f, forceY, 0f));
}
if (col.gameObject.name == “platformR”)
{
rb = this.GetComponent();
isGrounded = true;
forceY = 0f;
}
}
Hi there, the problem has been resolved.
here’s the code that works.
public class BallController : MonoBehaviour
{
Rigidbody rb;
public float forceY = 150f;
public bool isGrounded;
void Start()
{
rb = this.GetComponent<Rigidbody>();
isGrounded = false;
}
void Update()
{
if (isGrounded == true && Input.GetMouseButton(0)) /* correction 1 (==) */
{
rb.AddForce(new Vector3(10f, forceY, 0f));
}
}
public void OnCollisionEnter (Collision col) /* Correction 2 (OnCollisionEnter) */
[IMG]https://ssl.gstatic.com/ui/v1/icons/mail/images/cleardot.gif[/IMG]
{
if ([URL='http://col.gameobject.name/']col.gameObject.name[/URL] == "platformL")
{
rb = this.GetComponent<Rigidbody>();
isGrounded = true;
rb.AddForce(new Vector3(10f, forceY, 0f));
}
if ([URL='http://col.gameobject.name/']col.gameObject.name[/URL] == "platformR")
{
rb = this.GetComponent<Rigidbody>();
isGrounded = true;
forceY = 0f;
}
}
Please used code-tags when posting code and not plain text. You can edit your posts to include this too.
Glad you got it resolved.
Thanks.
Didn’t know about it. Thank you so much.
1 Like