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 Celledor · Oct 18, 2012 at 06:17 PM · c#cameraconversionmovment

Js to c# conversion

I'm trying to convert an old camera script from JS to C# but I'm duing something wrong as the camera behaves very strange. Anyone that can help me fix it?

The scripts can be tested by attach it to a Camera and setting the target transfrom to a sphere or something like that (it will be used as a rotation point).

The JS version:

 // Attach this to a Camera and it will be controlled using the following input
 //
 // W: Move camera forward
 // S: Move camera backwards
 // A: Strafe camera to the left
 // D: Strafe camera to the right
 //
 // Mouse scroll wheel: Zoom in and out
 //
 // While pressing Shift and moving the mouse: rotate and pan the camera around the target
 
 var target : Transform; // Is used as a rotation point
 var distance = 25;
 
 var xSpeed = 450.0;
 var ySpeed = 220.0;
 
 var yMinLimit = 30;
 var yMaxLimit = 89;
 
 private var x = 0.0;
 private var y = 0.0;
 
 var maxDist : float = 50;
 var minDist : float = 5;
 var zoomSpeed : float = 2;
 
 var speed : float = 50.0;
 var modifier : float=1.0;
 
 // Add this script so that it can be accessed from the Unity3d editor
 // It will be under the Component menu
 @AddComponentMenu("Camera-Control/Mouse Orbit")
 partial class MouseOrbit { }
 
 function Start () 
 {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
     // Make the rigid body not change rotation
     if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function LateUpdate () 
 {
     if (target) 
     {
         
         // Mouse pan
         if(Input.GetKey (KeyCode.LeftShift))
         {
             x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
         }
 
         y = ClampAngle(y, yMinLimit, yMaxLimit);
         
         // Update target rotation
         target.rotation = Quaternion.Euler(0.0, x, 0.0) ;
         
         // Update camera target position
         // Move forward and backward 
         var forward = target.forward * modifier * Time.deltaTime * speed * Input.GetAxis("Vertical");
         target.position+=forward;
         
         // Strafe right and left
         var right = target.right * modifier * Time.deltaTime * speed * Input.GetAxis("Horizontal");
         target.position+=right;
         
         //var up = Vector3.up * modifier * Time.deltaTime * speed;
         //target.position+=up;
         
         // Zoom in and out
         if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance < maxDist)
         {
             distance += zoomSpeed;                             
             transform.Translate(Vector3.forward * -zoomSpeed); 
         }
         else if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance > minDist)
         {
             distance -= zoomSpeed;                             
             transform.Translate(Vector3.forward * zoomSpeed); 
         }    
         
         // Restrict the target y position to the height of the terrain at the current target position
         target.position.y  = Terrain.activeTerrain.SampleHeight(target.position) + 1;
     
         // Update camera position    
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
 
         transform.rotation = rotation;
         transform.position = position;
         
         // Restrict the camera y position to the height of the terrain at the current camera position if the camera would be under the ground
         if(transform.position.y <= Terrain.activeTerrain.SampleHeight(transform.position) + 5)
             transform.position.y  = Terrain.activeTerrain.SampleHeight(transform.position) + 5;
     }
 }
 
 // Make sure the angle dosn't excede 0 - 360 degrees
 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);
 }


The C# version (that dont't work):

 using UnityEngine;
 using System.Collections;
 
 // Add this script so that it can be accessed from the Unity3d editor
 // It will be under the Component menu
 [AddComponentMenu("Transform/Follow Transform")]
 public class MouseOrbit : MonoBehaviour 
 {
     // Attach this to a Camera and it will be controlled using the following input
     //
     // W: Move camera forward
     // S: Move camera backwards
     // A: Strafe camera to the left
     // D: Strafe camera to the right
     //
     // Mouse scroll wheel: Zoom in and out
     //
     // While pressing Shift and moving the mouse: rotate and pan the camera around the target
     
     public Transform target; // Is used as a rotation point
     public float distance= 25f;
     
     public float xSpeed= 450.0f;
     public float ySpeed= 220.0f;
     
     public float yMinLimit= 30;
     public float yMaxLimit= 89;
     
     private float x= 0.0f;
     private float y= 0.0f;
     
     public float maxDist = 20;
     public float minDist = 5;
     public float zoomSpeed = 2;
     
     public float speed = 50.0f;
     public float modifier = 1.0f;
     
     void Start ()
     {
         Vector3 angles= transform.eulerAngles;
         x = angles.y;
         y = angles.x;
     
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
     
     void LateUpdate()
     {
         if (target) 
         {
             // Mouse pan
             if(Input.GetKey (KeyCode.LeftShift))
             {
                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
             }
     
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             
             // Update target rotation
             target.rotation = Quaternion.Euler(0.0f, x, 0.0f) ;
             
             // Update camera target position
             // Move forward and backward 
             Vector3 forward = target.forward * modifier * Time.deltaTime * speed * Input.GetAxis("Vertical");
             target.position += forward;
             
             // Strafe right and left
             Vector3 right = target.right * modifier * Time.deltaTime * speed * Input.GetAxis("Horizontal");
             target.position += right;
             
             //FIXME_VAR_TYPE up= Vector3.up * modifier * Time.deltaTime * speed;
             //target.position+=up;
             
             // Zoom in and out
             if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance < maxDist)
             {
                 distance += zoomSpeed;                             
                 transform.Translate(Vector3.forward * -zoomSpeed); 
             }
             else if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance > minDist)
             {
                 distance -= zoomSpeed;                             
                 transform.Translate(Vector3.forward * zoomSpeed); 
             }    
             
             // Restrict the target y position to the height of the terrain at the current target position
             Vector3 pos = target.position;
             
             pos.y = Terrain.activeTerrain.SampleHeight(target.position) + 1;
             target.position = pos;
         
             // Update camera position    
             Quaternion rotation = Quaternion.Euler(y, x, 0);
             
             Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -distance) + target.position);
             
             transform.rotation = rotation;
             transform.position = position;
             
             // Restrict the camera y position to the height of the terrain at the current camera position if the camera would be under the ground
             pos = target.position;
             if(pos.y <= Terrain.activeTerrain.SampleHeight(transform.position) + 5)
                 pos.y  = Terrain.activeTerrain.SampleHeight(transform.position) + 5;
             
             target.position = pos;
         }
     }
     
 
     //public static Vector3 r operator +(Vector3 a, Vector3 a)
     //{
 //           return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
 //    } 
 
     
     // Make sure the angle dosn't excede 0 - 360 degrees
     static float ClampAngle (float angle, float min, float max)
     {
         if (angle < -360)
             angle += 360;
         
         if (angle > 360)
             angle -= 360;
         
         return Mathf.Clamp (angle, min, max);
     }
 }
Comment
Add comment · Show 2
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 BerggreenDK · Oct 18, 2012 at 08:51 PM 0
Share

Do you get any compiler errors?

avatar image Celledor · Oct 18, 2012 at 10:16 PM 0
Share

No, the game runs but the camera behave in a totally different way than the js code. Its all messed up, something is not done right in the calcuations of the vectors but I can't find what.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nosyrbllewe · Oct 18, 2012 at 10:51 PM

Have you tried this? It may help fix it. 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

11 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

Related Questions

Distribute terrain in zones 3 Answers

JS to C#, anyone? 1 Answer

help declaring Transform in Find statement BCE0019 1 Answer

Multiple Cars not working 1 Answer

Change the background color attribute of a camera in C#? 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