- Home /
Creating a bounce effect
In essence I am creating a platform game, on the screen are three floors, my character (on the ground floor) should hit an obstacle and bounce back a short distance. However the character is bouncing back a long way and I cannot work out how to resolve it.
This is the script I have created
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
GameObject[] charaters = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject charater in charaters){
//get the CharaterControllerScript
CharaterControllerScript controlScript = (CharaterControllerScript)charater.GetComponent(typeof(CharaterControllerScript));
//stop all charater movement
controlScript.Move = 0;
}
GameObject charaterCollision = other.gameObject;
charaterCollision.rigidbody2D.position = new Vector2(1, charaterCollision.rigidbody2D.position.y);
}
}
I believe the problem with this is the last line of the "if" statement. I have tried several different options but the character keeps bouncing back to the beginning of the scene.
I need it to only bounce back a few pixels.
NB: I have tried adding a 2d material to the obstacle, but that bounce the character up to the next floor.
just a suggestion can't you use physics material and assign .
abi.kr01. can you please reword that, I don't understand
Answer by zach-r-d · Jun 11, 2015 at 08:32 AM
I believe you are correct. That '1' in the new Vector2 means that the character will be teleported to x-position 1 whenever the trigger is entered. Try something like this instead:
charaterCollision.rigidbody2D.position -= new Vector2(1, 0);
This will subtract 1 from whatever x-position the character is currently at.
Answer by saravanan-P · Jun 11, 2015 at 09:14 AM
add a physics2D material to the collider2D in the inspector..
And in that "Physics2D material"..change the Bounciness and friction,
for example, like this..
saravanan P, thank you, I did try that but it did not work for me
Your answer
Follow this Question
Related Questions
Player Bounces On Ascending Platform After Platform Velocity Is Updated 0 Answers
Does Unity come with a method that finds supplementary angles? 1 Answer
Input System: How to limit a Vector2 input to be only up, down, left or right. 2 Answers
Locking particle axis? 3 Answers
Object should be bouncier 2 Answers