- Home /
how to make trampoline in unity 3D
I am just making maze game that rotates stage to move the ball to move into a goal and I am planning to make trampoline that bounce a ball, however although I use other webpage's explanations, it seems not working? What's goes wrong and how can I fix this code to make ball bounce when ball collide the plate which contains this code below?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bouncing : MonoBehaviour
{
public float BouncingForce = 2f;
public Rigidbody PlayerBall;
private void Start()
{
PlayerBall = gameObject.GetComponent<Rigidbody>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
PlayerBall.AddForce(Vector3.up * BouncingForce);
}
}
Answer by TechnicPyro · Oct 17, 2020 at 09:43 AM
It seems that you are not actually referencing the ball object within your game.
On line 12 where you assign a rigidbody to the PlayerBall object it is not the ball's rigidbody being assigned. It is the rigidbody of the game object the script is attached to.
In order to reference the ball object you will have to use some other way to reference it.
Using GameObject.Find()
One way would be the Find() method.
To use it, you would do something like this:
GameObject.Find("NameOfYourBallObject").GetComponent<Rigidbody>();
This method uses "GameObject". The capitalization of the G changes its meaning compared to "gameObject". "GameObject" is just a general call to something of type GameObject within the scene.
The Find() method takes the name of the game object you are looking for as a string, so type the name of your ball object in there.
Using GameObject.FindWithTag()
This way would be similar to the Find() method.
GameObject.FindWithTag("TagOfBallObject").GetComponent<Rigidbody>();
Instead of inputting the name of the object as the parameters of the function, you would be using the tag assigned to it.
So long as you do either of these options in place of your 12th line it should work, just remember to change to parameters to match your object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613013302im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scripted Ball Bouncing igher and higher 0 Answers
How can I show trajectory of a bouncing ball OnCollisionEnter/OnCollisionExit? 0 Answers
Multiple Cars not working 1 Answer
Storing gameObjects in an array and essentially printing them? 1 Answer
Replacing mesh collider with primitive colliders via script 1 Answer