- Home /

**closed**Dec 04, 2014 at 05:05 AM by Prasanna for the following reason:

The question is answered, right answer was accepted

# 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

Do it then.

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

sorry man I did it as a comment instead 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)

**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

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

using UnityEngine; using System.Collections;

public class CameraRotation : MonoBehaviour { public Transform Target;

```
public float edgeBorder = 0.1f;
public float horizontalSpeed = 0.1f;
public float verticalSpeed = 0.1f;
public float minVertical = 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, minVertical, 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 min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
```

}

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;
```

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

float'

it's this bit:

```
static void ClampAngle (float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, 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 min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
```

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

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Natural camera sway 1 Answer

Animated camera problems 0 Answers

WorldToScreenPoint 0 Answers

small example sendmessage from javascript to C# wrong??? 1 Answer

C# trigger problem 1 Answer