Random Object Selection
So I got this cool spell idea dealing with telekinesis. Your standing near a pile of various objects all cluttered together, you press the fire button, the computer randomly selects one object from the clutter and flings it across the screen like a force push move. You press the fire button again, the computer randomly selects another object from the clutter and flings it across the screen again, so on and so forth, every time I press the fire button.
Now I have some parts of the program to do this, fling objects using AddForces and RigidbBody Triggers around the various objects in the pile just to make the computer realize when your standing near the pile or not for the action to take place. Code to aim/point the objects in different directions.
The only part I don't have is to randomly select one individual object from the pile/clutter at a time. I have a rough idea how the programming code I need might be, such as using Random Range, but how to config this code into what I want? Also another issue, idk if it's right or wrong, but I think every object in the pile needs a number, a tag or marker id, name fixed to it and then the computer cycles through these numbers, tags, etc, selects one of them, selects the one object in the pile attached to it, then sets it as the object to have action applied to it such as flinging it.
In the end how to randomly select one object at a time? The code is included.
UnityEngine; using System.Collections;
public class field1 : MonoBehaviour { public Transform playerCam; public float throwForce = 10;
private void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown(KeyCode.Mouse1))
GetComponent<Rigidbody>().AddForce(playerCam.forward * throwForce);
if (Input.GetKeyDown(KeyCode.Mouse1))
GetComponent<Rigidbody>().angularVelocity = new Vector3(4, 4, 4);
}
}`
Your answer
Follow this Question
Related Questions
Hello, Need help with a problem 1 Answer
How to lock selection to an object in the scene view 0 Answers
How do you highlight an object pointed to by a raycast unity 3d? 2 Answers
Spawning an object betwheen two positions (while making sure whole object is within area) 1 Answer
How do I allow 3rd Person Character Picking Up Objects within a range? 1 Answer