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.

add a physics2D material to the collider2D in the inspector…

And in that “Physics2D material”…change the Bounciness and friction,

47995-pm.png

for example, like this…

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.