- Home /
Make an object "bounce" off walls?[Solved]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ebSpreadSpawner : MonoBehaviour {
public GameObject Bullet;
private GameObject spawner;
private Transform circleTrans;
public int numberOfObjects = 24;
private static float spawnTime;
public bool isLeft;
public bool isRight;
void Live()
{
if (spawnTime <= 0)
{
float radius = 2.5f;
for (int i = 0; i < numberOfObjects; i++)
{
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3 (Mathf.Cos (angle), Mathf.Sin (angle), 0) * radius;
GameObject b = Instantiate (Bullet, circleTrans.position - pos, Quaternion.identity) as GameObject;
b.transform.parent = circleTrans;
b.name = "Bullet";
}
spawnTime = 2.5f;
}
}
void Start()
{
circleTrans = transform;
spawnTime = 5.0f;
spawner = GameObject.Find ("Spawner");
}
void OnTriggerEnter(Collider other)
{
if (other.collider.tag == "rightBumper")
{
Debug.Log("IsRight");
isRight = true;
isLeft = false;
}
if (other.collider.tag == "leftBumper")
{
Debug.Log("IsLeft");
isLeft = true;
isRight = false;
}
}
void LateUpdate ()
{
if (isLeft)
{
this.transform.position += new Vector3 (-3.0f * Time.deltaTime, -1.0f * Time.deltaTime, 0.0f);
}
if (isRight)
{
this.transform.position += new Vector3 (3.0f * Time.deltaTime, -1.0f * Time.deltaTime, 0.0f);
}
//Live ();
spawnTime -= Time.deltaTime;
}
}
So: It looks like the Bulelt object that gets spawned from it, causes it to activate the isLeft and isRight bools. Which makes it get all jittery.
So... Layers?
Yup.. Layers....
MAde a new layer called "bullet" and disabled all from it in the Physics menu.
Jitteryness is gone now..
Your Update() should be changed to LateUpdate()
when you're handling with physics in scripting. Other than physics, most things can go in Update.
When is the jittering happening? When it collides? Does it "stick" to the wall when it jiggles?
It happens when an object collides, and it doesn't stick, just change direction as if that object had hit the wall.
I built a test scene with your provided code (except for the Y change of position) and it works flawlessly.
I have a rigidbody that does not apply gravity attached to a sphere with the supplied code attached to it. The leftBumper is to the left and the rightBumper is to the right. The sphere bounces between them without jiggle anywhere.
The code I'm using:
bool isRight = true;
bool isLeft = false;
void OnTriggerEnter (Collider other)
{
if (other.collider.tag == "rightBumper") {
Debug.Log ("IsRight");
isRight = true;
isLeft = false;
}
if (other.collider.tag == "leftBumper") {
Debug.Log ("IsLeft");
isLeft = true;
isRight = false;
}
}
void Update ()
{
if (isLeft) {
this.transform.position += new Vector3 (-3.0f * Time.deltaTime, 0f, 0f);
}
if (isRight) {
this.transform.position += new Vector3 (3.0f * Time.deltaTime, 0f, 0f);
}
}
$$anonymous$$y conclusion is that it has to be something else causing it.
In addition, I just tried re-adding the Y code in and used two very long boxes as bumpers and it still works flawlessly.
Are you using a rigidbody and if so, what are the settings for it?
I do havea rigidbody on the moving object:
$$anonymous$$ass 1 Drag 0 Use Gravity false is $$anonymous$$inematic false interpolate none collision detection discrete constraints fronzen on Z transform and xyz rotation
Your answer
![](https://koobas.hobune.stream/wayback/20220613183624im_/https://answers.unity.com/themes/thub/images/avi.jpg)