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 /
This question was closed Dec 04, 2014 at 05:05 AM by Prasanna for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Prasanna · May 02, 2014 at 12:40 PM · c#camerajavascriptconvert

Script Convert

Hello there, I have a java script for camera rotation. But i want to convert that code into C#. Here is the Code...

 var target : Transform;
 
 var edgeBorder = 0.1;
 
 var horizontalSpeed = 360.0;
 
 var verticalSpeed = 120.0;
 
 var minVertical = 20.0;
 
 var maxVertical = 85.0;
 
 
 
 private var x = 0.0;
 
 private var y = 0.0;
 
 private var distance = 0.0;
 
 function Start()
 
 {
 
     x = transform.eulerAngles.y;
     y = transform.eulerAngles.x;
     distance = (transform.position - target.position).magnitude;
 
 
 }
 
 function LateUpdate()
 
 {
 
     var dt = Time.deltaTime;
     x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
     y += Input.GetAxis("Vertical") * verticalSpeed * dt;
     
     y = ClampAngle(y, minVertical, maxVertical);
      
     var rotation = Quaternion.Euler(y, x, 0);
     var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
     
     transform.rotation = rotation;
     transform.position = position;
 
 
 }
 
 static function ClampAngle (angle : float, min : float, max : float) 
 
 {
 
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return Mathf.Clamp (angle, min, max);
 
 }

Please explain this...

-Prasanna

Comment
Add comment · Show 5
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 ffxz7ff · May 02, 2014 at 12:46 PM 2
Share

Do it then.

I'll go block UA in my hosts file now. Enough is enough.

avatar image Prasanna · May 02, 2014 at 01:22 PM 0
Share

What are u talking about?

avatar image Prasanna · May 02, 2014 at 01:51 PM 0
Share

Thank u man...

avatar image dovonobob · May 02, 2014 at 01:54 PM 0
Share

sorry man I did it as a comment ins$$anonymous$$d of an answer so I've moved it :)

also you might need to put the word 'new' on a few of those lines but I'm not in front of a machine to test it to be sure.

for instance say you were setting a Vector by using a new vector:

e.g.

Vector3 vector = new Vector3(0, 0, 0)

avatar image robertbu · May 02, 2014 at 03:31 PM 0
Share

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

http://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by dovonobob · May 02, 2014 at 01:52 PM

god knows what that previous comment means......

It's not that different really just how you declare the variables, you have to specify the type of variable. So for instance your time variable:

var dt

would be declared in c# as:

float dt

so:

var position

would be:

Vector3 position

and your rotation:

Quaternion rotation

Comment
Add comment · Show 6 · 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 Prasanna · May 05, 2014 at 06:11 AM 0
Share

Hi i tried this code, is this Correct...

using UnityEngine; using System.Collections;

public class CameraRotation : $$anonymous$$onoBehaviour { public Transform Target;

 public float edgeBorder = 0.1f;    

 public float horizontalSpeed = 0.1f;
 
 public float verticalSpeed = 0.1f;
 
 public float $$anonymous$$Vertical = 0.1f;
 
 public float maxVertical = 0.1f; 
     
 float x = 0.0f;    

 float y = 0.0f;
 
 float distance = 0.0f;
 
 void Start()    
 {
     x = transform.eulerAngles.y;
     y = transform.eulerAngles.x;
     distance = (transform.position - target.position).magnitude;
 }
 
 void LateUpdate()    
 {    
     float dt = Time.deltaTime;
     x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
     y += Input.GetAxis("Vertical") * verticalSpeed * dt;
     
     y = ClampAngle(y, $$anonymous$$Vertical, maxVertical);
     
     Quaternion rotation = Quaternion.Euler(y, x, 0);
     Vector3 position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
     
     transform.rotation = rotation;
     transform.position = position;
 }
 
 static void ClampAngle (float angle, float $$anonymous$$, float max)     
 {    
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);    
 }

}

avatar image dovonobob · May 06, 2014 at 10:09 AM 0
Share

I'm not sure without trying it, did Unity error when you compiled it?

the only one line that I see might be a problem is:

 Vector3 position = rotation * Vector3(0.0, 0.0, -distance) + target.position;

might need to do:

 Vector3 position = rotation * new Vector3(0.0, 0.0, -distance) + target.position;
avatar image Prasanna · May 06, 2014 at 02:01 PM 0
Share

Thanks man. i will try

avatar image Prasanna · May 06, 2014 at 02:04 PM 0
Share

I have this Error.. Assets/CameraRotate.cs(30,17): error CS0029: Cannot implicitly convert type void' to float'

avatar image dovonobob · May 07, 2014 at 11:06 AM 0
Share

it's this bit:

 static void ClampAngle (float angle, float $$anonymous$$, float max)  
 {   
     if (angle < -360)
        angle += 360;
     if (angle > 360)
        angle -= 360;
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max); 
 } 

you're running a function that is set to return nothing (the 'void' bit) but you then return 'mathf.clamp'.

Change the function to this:

     static float ClampAngle (float angle, float $$anonymous$$, float max)  
     {   
         if (angle < -360)
            angle += 360;
         if (angle > 360)
            angle -= 360;
         return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max); 
     } 
avatar image dovonobob dovonobob · May 07, 2014 at 11:08 AM 0
Share

also mark the answer as correct I'm lonely and need the karma ;)

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert this string formatting from C# to JS 1 Answer

convert some c# to javascript please 0 Answers

Does C# use #pragma strict? 2 Answers

C# to UnityScript conversion help. 0 Answers

Trouble with converting some JS code to C# 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