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 hyderox · Aug 12, 2012 at 03:12 PM · c#camerajavascriptview

Please Help me converting this to C#

I have this little code which don't let the camera view be blocked, but this is in JS. Can you help me translate this to C#? I'm allways haveing a few errors in mine.

// IS VIEW BLOCKED?

 var rotation:Quaternion = Quaternion.Euler(y, x, 0); 
 var hit : RaycastHit; 

 var trueTargetPosition : Vector3 = target.transform.position - Vector3(0,-targetHeight,0);

 // Cast the line to check:

 if (Physics.Linecast (trueTargetPosition, transform.position, hit)) {  

     // If so, shorten distance so camera is in front of object:

     var tempDistance = Vector3.Distance (trueTargetPosition, hit.point) - 0.28; 

     // Finally, rePOSITION the CAMERA:

     position = target.position - (rotation * Vector3.forward * tempDistance + Vector3(0,-targetHeight,0)); 

     transform.position = position;
Comment
Add comment · Show 1
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 TheVectorHunter · Aug 12, 2012 at 04:22 PM 0
Share

What are the "few errors"? C# is way more strict than Unityscript so don't expect it to be any easier.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Muuskii · Aug 12, 2012 at 04:46 PM

Woah! There is a lot here that doesn't make any sense.

     void Update()   //You didn't have any functions! I'm assuming you need this is in Update
     {
      
      // IS VIEW BLOCKED?
      Transform target;    //You never defined this!! 
      Quaternion rotation = Quaternion.Euler(y, x, 0);   //where do these values come from?
      RaycastHit hit; 
      Vector3 trueTargetPosition = target.transform.position - new Vector3(0,-targetHeight,0);//you needed "new"
      
      // Cast the line to check:
      //use Raycast, and you had the vectors backwards
      if (Physics.Raycast (transform.position, trueTargetPosition - transform.position, out hit)) //out modifier is needed
      {
          //Vector3 tempDistance = Vector3.Distance (trueTargetPosition, hit.point) - 0.28;  //no
          //This is ALWAYS more helpful
          Vector3 Difference = transform.position - hit.point;   //Vector from hit.point
      
          // If so, shorten distance so camera is in front of object:
          Difference = Difference.normalized * (Difference.magnitude - 0.28f);  //you needed the f
      
          // Finally, rePOSITION the CAMERA:
      
         /* position = target.position - (rotation * Vector3.forward * tempDistance + Vector3(0,-targetHeight,0));*/ 
 //Where does "position" and "rotation" come from??
      
          transform.position = target.position + Difference;
          }  //You were missing this bracket
      
      }
  
  
 }


Honestly some of the stuff in here makes it look like you barely even tried to code this in C# just saw a whole bunch of errors and posted the code without reading them.

I can't test this at the moment, please tell me how it goes from here.

Comment
Add comment · Show 3 · 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 TheVectorHunter · Aug 12, 2012 at 04:53 PM 0
Share

He wasn't coding in C#, it was Unityscript.

avatar image Muuskii · Aug 12, 2012 at 05:03 PM 0
Share

Is that why the title of the question was "Help me convert this to C#"

avatar image TheVectorHunter · Aug 12, 2012 at 05:05 PM 0
Share

Was that sarcasm? :)

avatar image
0

Answer by hyderox · Aug 12, 2012 at 07:54 PM

Thank you for the help! As you can see I did not post my full script, only this part. I tried to translate it to C# but I had errors and I couldn't fix it.

Comment
Add comment · 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
0

Answer by Meltdown · Aug 12, 2012 at 08:25 PM

Use this code converter. It's not perfect but it does all the plumbing for you

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

Comment
Add comment · 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

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a strategy camera go up and down? 1 Answer

Isometric camera skybox not working. 0 Answers

Connection between 2 scripts 1 Answer

How to mimic Unity Scene Gizmo? 2 Answers

Visibility of objects 2 Answers


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