- Home /
Cube bounces off wall
Hello! I am making a game where a cube runs into a wall. I need the cube to just stay at the wall when it hits it, but instead it slowly starts to fade away from the wall, I believe from bouncing/ How would I fix this issue? Thanks.
Answer by Konomira · Dec 20, 2017 at 10:42 PM
Assuming you're using Rigidbody, you can do
void OnCollisionEnter(Collision c)
{
GetComponent<RigidBody>().velocity = Vector3.zero;
}
This will stop the object entirely when it reaches wall.
Answer by awesomeguy9807 · Apr 22, 2018 at 12:45 PM
Make a new script for the cube and in MonoBehaviour, make a void OnCollisionEnter and put a name in brackets (I suggest "other") like this:
//the rate at which your cube moves has a speed, so there must be a
//public float in your script for that variable (just call it speed or velocity). So
//just use that variable in the code
void OnCollisionEnter(Collision name)
{
//Set the velocity of the cube to zero using its RigidBody
//If it is hitting the wall from the x axis
transform.position.x = //cube's X position when it hits the wall
//If it is hitting on the z axis
transform.position.z = //cube's Z position when it hits
}
Tell me if that works.
Your answer
Follow this Question
Related Questions
Making a wall of cubes 2 Answers
Cube glitches when it moves into wall 2 Answers
Cycle Through Array - Change Values 0 Answers
How to make a wall inpassible 1 Answer
Dynamically tiling texture on a cube of changing scale. 1 Answer