- Home /
GetComponent help for C#. How to have object at edge of camera view
Hello I'm new to C#. The following code works in UnityScript with slight alterations. However, I can't seem to get it to work in C#. I've tried an online converter, as well as searching Unity Answers for a fix, but I haven't had much luck.
edge.transform.position = GetComponent< Camera > ()ViewportToWorldPoint(Vector3(1f, 0.5f, -zpos));
I already got a workaround by copying an example from the scripting reference, but I would like to know if it is possible to achieve this in one line in C# like it is in UnityScript. For anybody that comes across my question looking for an answer, here is the working code that you can use to achieve your goal
Camera camera = GetComponent<Camera>();
edge2.transform.position = camera.ViewportToWorldPoint(new Vector3(1f, 0.5f, -zpos));
P.S. So far C# seems to add a lot of "unnecessary" steps. I might just stick to UnityScript even if it doesn't have any other function outside of Unity
Answer by hexagonius · Sep 22, 2017 at 07:38 PM
Of course you can
edge2.transform.position = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(1f, 0.5f, -zpos));
You mean all I had to do was add a period after ()? Haha this is too funny. I spent between 45$$anonymous$$s-1hour on this
Quick follow up question, theres no difference between 1f and 1.0f in C# right?
They're the exact same but if 1f is pointless since it could just be 1 and 1.0f is a number with a decimal so yes they're the same.
Your answer
Follow this Question
Related Questions
Screen Boundary Solutions 0 Answers
Viewport to World Coordinates 2 Answers
ViewportToWorldPoint with multiple cameras 0 Answers
Why is there no camera attached? 1 Answer