- Home /
Make an object move towards random spot on another objects edge?
I'm working with a script that performs a waypoint setup, object moves from A to B to C to D to A... etc.
Right now the object, in this case a pool cue ball, moves towards the bumpers middle position and on to the next bumpers middle. Is there an easy way to randomize the position that it moves towards instead of using the center of the object?
You can see below the green lines are the gizmos I have setup to show the path from waypoint to waypoint, and the red is where i'd like it to randomly strike, just a few examples of some random points.
This is the code I'm using for the Follow script that the ball has set on it.
using UnityEngine;
using System.Collections;
public class Follow : MonoBehaviour {
public Transform activeWaypoint;
public Vector3 move;
public float speed = 1.0f;
void OnTriggerEnter (Collider triggerWaypoint) {
if (activeWaypoint.collider == triggerWaypoint) {
activeWaypoint = activeWaypoint.GetComponent<Waypoint>().next;
move.x = (activeWaypoint.transform.position.x - transform.position.x) / speed;
move.z = (activeWaypoint.transform.position.z - transform.position.z) / speed;
}
}
void Start () {
activeWaypoint = Waypoint.start;
move.x = (activeWaypoint.transform.position.x - transform.position.x) / speed;
move.z = (activeWaypoint.transform.position.z - transform.position.z) / speed;
}
// Update is called once per frame
void Update () {
transform.Translate(move * Time.deltaTime);
}
}
I'm just not familiar enough with the rendering or object bounds to get a random point along an objects edge. Any help would be appreciated! :D
Answer by Strom_CL · Nov 17, 2012 at 07:14 AM
Figured it out finally, I just used:
move.x = (activeWaypoint.transform.position.x +
Random.Range(-activeWaypoint.renderer.bounds.extents.x + 1,
activeWaypoint.renderer.bounds.extents.x - 1)
- transform.position.x)
This way it uses the activeWaypoint position then adds a random negative value from the extents (half the length) to a positive extents length. I did a +1 and -1 to keep the ball away from the edge, was getting bounces right on the opening. Works like a charm! :D
Might be an easier way but this works, still open to any ideas!
that's just how you do it, but don't forget it only works if the object is horizontal or vertical.
for a more general solution just DO THIS:
under the object add two $$anonymous$$AR$$anonymous$$ER OBJECTS. I mean simply two empty game objects. Name them "$$anonymous$$arkerLeftEnd" and "$$anonymous$$arkerRightEnd"
In video game dev, it is incredibly common to have "marker objects" everywhere in the scene. I mean on say a vehicle, you could easily have dozens. (things like "attract bugs to here" "keyhole near hear" "top of roofline" etc etc .. it goes on and on)
So then write a general script that returns a random point between the two such markers.
This is an incredibly useful general solution. Note that you can very easily move the markers by eye .. for instance you may actually not want the "very ends" of the object -- whatever. hope it helps.
Your answer
Follow this Question
Related Questions
2D get touch input 1 Answer
Object not moving,Object not moving in any direction 1 Answer
Position an array of objects randomly 1 Answer
How to get an Objects position per axis? 1 Answer
Save object position (random) 1 Answer