Question is off-topic or not relevant
Add force random movement and random rotation [2D]
I have a box and inside three 2D sprites. The walls of the box have bounce surface material.
What I would like is these three sprites to move with random speed at random direction and every time one of them hits a wall to change rotation and move away, all this while slowly falling to the lower part of the box (cause of physics drag).
I prefer c# solutions and thank you for your time!
Please refer to the FAQ and User Guide. "Write my code for me" questions aren't appropriate for Unity Answers.
Ok this is noted please remove Zoogyburger's code from my question and I will post my work on the problem for others to answer even though community is filled with similar questions and no code
Its unfortunate this was closed because there's barely 3 lines of code to write here. Anyway this was the answer I tried to post... maybe it'll help you:
I'm not really sure what you're after here, but I'll try. Things dont move to "a lower part of the box" from drag, maybe from gravity though.
So assu$$anonymous$$g you have bouncy phys-materials on all objects, and a very low gravity constant (0.01 maybe?) all you would need to actually code is the initializing part ("random speed at random direction") and the rotate-on-bounce part. So here's that as a script, which you would attach to all 3 sprites:
const float $$anonymous$$AX_STARTING_SPEED = 50;
class BouncingSprite : $$anonymous$$onoBehaviour {
void Start() {
GetComponent<Rigidbody2D>().velocity = Random.insideUnitCircle * (0.5f + 0.5f * Random.value) * $$anonymous$$AX_STARTING_SPEED;
}
void OnCollisionEnter2D(Collision2D collision) {
transform.localRotation = Quaternion.Euler(Random.value * 360, Random.value * 360, Random.value * 360);
}
}
If this isn't quite what you were looking for, you should add more details to your question to explain what you're actually trying to do.
Thank you this actually gave me a very good start, too bad that they closed this for no reason
Answer by Zoogyburger · Feb 26, 2016 at 07:00 PM
You shouldn't ask people to write you code here. Here's an example:
public float moveSpeed;
public float timeBetweenMove;
private float timeBetweenMoveCounter;
public float timeToMove;
private float timeToMoveCounter;
private Rigidbody2D myrigid;
private bool moving;
private Vector3 moveDirection;
void Start()
{
myrigid = GetComponent<Rigidbody2D>();
timeBetweenMoveCounter = Random.Range (timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range (timeToMove * 0.75f, timeToMove * 1.25f);
}
void Update()
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myrigid.velocity = moveDirection;
if (timeToMoveCounter < 0f)
{
moving = false;
timeBetweenMoveCounter = Random.Range (timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
}
}else {
timeBetweenMoveCounter -= Time.deltaTime;
myrigid.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0f)
{
moving = true;
timeToMoveCounter = Random.Range (timeToMove * 0.75f, timeToMove * 1.25f);
moveDirection = new Vector3 (Random.Range (-1f, 1f) * moveSpeed, Random.Range (-1f, 1f) * moveSpeed, 0f);
}
}
}
well I am sorry you feel that way, please remove your code I wont use this