Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Student · Apr 15, 2011 at 09:17 AM · cameratarget

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; }}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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; } }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Student · Apr 15, 2011 at 05:37 PM 0
Share

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?

avatar image Student · Apr 15, 2011 at 05:44 PM 0
Share

Got it! With following line thank you very much again.. $$anonymous$$ytargetPosition=myhit.transform;

avatar image
1

Answer by robertmathew · Apr 15, 2011 at 11:16 AM

http://www.m2h.nl/files/js_to_c.php

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Student · Apr 15, 2011 at 05:47 PM 0
Share

Hi Robert, thank you for the tool but it is not succesfull during converting the variables. What do you think?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges