- Home /
Dynamic instantiation of object with respect to another movingObject
Hi Everyone, I am trying to do an infinite boat riding game. I'm ok with the infinite moving and different animations that are required. The motto of the game is-> player has to throw the ball into a destiny while the boat is moving in forward direction. For that the destinies should be generated randomly, so that player has to throw the ball into that. the player will keep on moving.Every time I have to instantiate the destinies a bit infront of the player. So how can I access the dynamic position to create those.Any suggestions...? Thanx in advance.
Answer by robertbu · Jul 16, 2013 at 07:25 AM
This will depend on your setup. If you move your object in the forward direction, then you can use:
var nextPos = transform.position + transform.forward * distance;
If you don't move towards forward and are using Translate(someDirection) to move the object, you can do:
var nextPos = transform.position + someDirection.normalized * distance;
If you are using a rigidbody, and the rigidbody does not move towards the objects 'forward' you can do:
var nextPos = transform.position + rigidbody.velocity.normalized * distance;
Where 'distance' is the distance in front of the moving object for the next spawn. 'nextPos' would then be used as the position in your Instantiate() call.
Answer by Rams · Jul 16, 2013 at 12:20 PM
Thanx for your reply. I am about to generate container few distance infront of the boat, every time.
public Transform bucket; //this is the destiny to be put infront of the boat
public Transform gameObject; //this is the boat that travels forward
public Vector3 go;//this is for setting up the position every time
void Start () {
gameObject.transform.Translate(-7.624812f, -0.2119637f,-4.225473f);// for setting up the boat position on the water properly
}
void Update () {
gameObject.position = transform.right*0.02f + transform.position;
go = transform.position + transform.right* 2;
Hold();
Instantiate(bucket,go,Quaternion.identity);
if(bucket != null)
Destroy(bucket,3);//should be deleted after 3 sec.
}
IEnumerator Hold(){
print (Time.time);
yield return new WaitForSeconds(3);
print (Time.time);
}
Now the buckets havebeen generating infinitely at a same location and they are destroying.can u please help me...!