- Home /
How to make an actor spawn at a fixed position
I am making an app where I want the player to be just a little bit off the left side of the screen. When I play in unity my player is just a little bit the the right of the left edge. But when I test on my phone, the player is halfway off the screen. How do I make it consistent throughout all devices so that it is always on the screen but close to the end?
Answer by robertbu · Oct 13, 2014 at 08:15 PM
Solving just for position, use Viewport coordinates. Viewport coordinates start at (0,0) in the lower left of the screen and go to (1,1) in the upper right:
transform.position = Camera.main.ViewportToWorldPoint(Vector3(0.5, 0.5, 10.0));
This line (Javascript/UnityScript) would place the pivot point of the object at the center of the screen 10 units from the camera.
@robertbu how would that translate into csharp? I cant find anything like viewporttoworldport in monodevelop
The function is the same, but you have to do the C# things like 'new' and 'f':
transform.position = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 10.0f));
Here is the reference page for ViewportToWorldPoint():
http://docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html
@robertbu I tried doing that but I still get the same error: Assets/Scripts/$$anonymous$$ovement.cs(27,38): error CS0103: The name `ViewportToWorldPoint' does not exist in the current context
The code I typed is:
transform.position = ViewportToWorldPoint (new Vector3 (0.1f, 0.1f, 10f));
@robertbu sorry I am stupid.. I didnt write Camera.main in front of the stuff. Thanks for all your help!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Third person movement similar to Max Payne 1 Answer
Raycast Destroys player. 1 Answer
Choose random background at start 1 Answer