- Home /
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 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)
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 : $$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);
}
}
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 $$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);
}
also mark the answer as correct I'm lonely and need the karma ;)
Follow this Question
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