Hi all,
I have been investigating this for a while now and cannot find a solution.
I have an object that is instantiating on the left side of the screen (which I have scripted successfully).
On the instantiated object script, all i want it is for that instantiated object to move towards the right side of the screen at a random point along the y axis.
I have tried using:
transform.position = Vector3.MoveTowards (transform.position, Random.Range(2, 3), speedStar);
This doesn’t work (as I expected) because the second argument requires a target point:
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
I have tried looking through the unity script reference and cannot find a solution. If anyone knows the solution to this, I will be very grateful.
Edit: If it helps, I am making a 2D game for the iPad
Kind Regards
Nima
You could generate a random Y within the screen by doing
float randomY = Camera.main.ScreenToWorldPoint( new Vector3( 0, Random.Range( 0, Screen.pixelHeight ), 0 ) ).y;
or you could not use the y component, seeing as 0 on the screen is on the left, and use it directly as the spawn point.
See: Unity - Scripting API: Camera.ScreenToWorldPoint
I may be understanding this script wrong. I have tried to implement it but I am having no luck.
The spawning has been taken care of on another object (GameAttribute) and I can see the object (Star) being spawned at random points along the y axis on the left side of the screen.
I don’t quiet understand how Camera.ScreenToWorldPoint works.
I am implementing it like this:
void Update () {
Vector3 p = camera.ScreenToWorldPoint(new Vector3(100, 768, camera.nearClipPlane));
transform.position = Vector3.MoveTowards (transform.position, p, speedStar);
}
I am obviously making a silly mistake somewhere and you are probably thinking about shouting at me for my naivety :s