- Home /
Camera and Target JS to CS converting
I'm trying to convert js code(cred. azmundai) to c# in the following; I'm newcomer, could you please point me to the right way.. Thank you.
var cameraOffset = Vector3(1,1.414214,1); function Update () { var MyCamera = GameObject.Find("Main Camera"); var MytargetPosition = MyCamera.transform.position; var ray = camera.main.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { MytargetPosition = hit.transform.position; }
if(Input.GetButtonDown("Mouse0"))
{
camera.main.transform.position = MytargetPosition + cameraOffset;
}
}
to
UnityEngine; using System.Collections; public class hitted : MonoBehaviour { RaycastHit myhit = new RaycastHit(); Ray myray = new Ray(); private GameObject mycamera; private Transform MytargetPosition; void Update(){ mycamera=GameObject.Find("Main Camera"); myray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(myray, out myhit, 1000.0f) && Input.GetMouseButtonDown(0)) print(myhit.collider.name); Debug.DrawLine(myray.origin, myhit.point); MytargetPosition.transform.position = myhit.transform.position; mycamera.transform.position=MytargetPosition.transform.position; }}
Answer by Bunny83 · Apr 15, 2011 at 11:00 AM
I'm not sure what MytargetPosition
variable is good for. as private var you can't assign it from outside and internally you don't assign anything to it (resulting in nullref-exception). I've optimised it a bit. You also don't need to initialise RaycastHit since it's an out parameter.
using UnityEngine; using System.Collections;
public class hitted : MonoBehaviour { private RaycastHit myhit; private Ray myray; private GameObject mycamera = null; public Transform MytargetPosition; void Update() { if (mycamera == null) mycamera = GameObject.Find("Main Camera"); myray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(myray, out myhit, 1000.0f) && Input.GetMouseButtonDown(0)) print(myhit.collider.name); Debug.DrawLine(myray.origin, myhit.point); if (MytargetPosition != null) MytargetPosition.position = myhit.transform.position; mycamera.transform.position = myhit.transform.position; } }
Thanks Bunny83, I put a sphere and cube into scene. when I clicked to the cube camera positioned properly but clicked to the sphere, the sphere moved to where cube is. By the way I did put the "cube" to script's mytarget position variable initialy(as dropped down). What am I doing wrong?
Got it! With following line thank you very much again.. $$anonymous$$ytargetPosition=myhit.transform;
Answer by robertmathew · Apr 15, 2011 at 11:16 AM
Hi Robert, thank you for the tool but it is not succesfull during converting the variables. What do you think?
Your answer
Follow this Question
Related Questions
Moving Camera With 2 Players 3 Answers
Camera focusing on an object specified in a script 1 Answer
Camera re-target 2 Answers
(C#) Follow target (script) transform target PREFAB 0 Answers
Camera Target Switch By Tag HELP! 1 Answer