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 meena · Nov 15, 2011 at 03:10 AM · javascripttranslate

need help translating camera control script from C# to javascript

Hi all,

So I found this MouseOrbitZoom script on the Unify Community Wiki site, and need help translating it into Javascript; here's what I have so far:

 var target = Transform;
 var targetOffset = Vector3;
 var distance = 5.0;
 var currentDistance = float;
 var desiredDistance = float;
 var maxDistance = 20;
 var minDistance = .6;
 var xSpeed = 200.0;
 var ySpeed = 200.0;
 var yMinLimit = -80;
 var yMaxLimit = 80;
 var zoomRate = 40;
 var panSpeed = 0.3;
 var zoomDampening = 5.0;
 
 var position = Vector3;
 var rotation = Quaternion;
 var currentRotation = Quaternion;
 var desiredRotation = Quaternion;
 var angles = transform.eulerAngles;
 
 private var x = 0.0;
 private var y = 0.0;
    
 
 function Start () {
 
     if (!target){
               GameObject go = new GameObject("Cam Target");
             go.transform.position = transform.position + (transform.forward * distance);
             target = go.transform;
     }
         
     distance = Vector3.Distance(transform.position, target.position);
     currentDistance = distance;
     desiredDistance = distance;
 
     position = transform.position;
     rotation = transform.rotation;
     currentRotation = transform.rotation;
     desiredRotation = transform.rotation;
 
     x = angles.y;
     y = angles.x;
     
 }
 
 function LateUpdate (){
     
     if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl)){
     desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate*0.125f * Mathf.Abs(desiredDistance);
     }
     
     if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt)){
     
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
     
         y = ClampAngle(y, yMinLimit, yMaxLimit);
     
         desiredRotation = Quaternion.Euler(y, x, 0);
         currentRotation = transform.rotation;
            
         rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
         transform.rotation = rotation;
     }
  
     else if (Input.GetMouseButton(2)){
         
         target.rotation = transform.rotation;
         target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
         target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
     }   
     
     desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
     
     desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
         
     currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
 
     position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
     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);
 }

but get an error message for the GameObject go = ...blah line. If I change that line to read var go = new GameObject("Cam Target"); I get a whole list of errors.

Any ideas?

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

1 Reply

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

Answer by Default117 · Nov 15, 2011 at 06:35 AM

The way you have been declaring your variables is incorrect for starters. For instance you're stating var target = Transform; where the convention is 1. var 2. your variable name 3. : 4. the type 5. = 6. it's initialised data. So try doing things like var target : Transform; and var xSpeed : float = 200.0f; instead.

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 meena · Nov 15, 2011 at 11:17 PM 0
Share

thanks Dewald117, it worked!
for anybody that wants, I can post the script, but its too long - won't let me post i tin this comment space.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

About "translating" js into C# 1 Answer

CS0246: The type or namespace name `targetPosition' could not be found. Are you missing a using directive or an assembly reference? 1 Answer

C# to java translation 2 Answers

Using .Translate() on one variable moves a different var, how? and how do i stop this? 1 Answer

Translate Unity-scripts to uScript 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