- Home /
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);
}
}
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.
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
Your answer
Follow this Question
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