- Home /
Quaternion from US to C#
Hi, I have this script in US, I have been trying to translate it to C# but I don't know how to use quaternions, eulers and that stuff I hope you can help me here is the code in JS and below the code in C#, thanks in advance.
#pragma strict
var qTo : Quaternion;
var speed = 1.25;
var rotateSpeed = 3.0;
var timer = 0.0;
function Start()
{
qTo = Quaternion.Euler(Vector3(0.0,0.0, Random.Range(-30, 30)));
}
function Update() {
timer += Time.deltaTime;
if(timer > 2)
{
qTo = Quaternion.Euler(Vector3(0.0,0.0,Random.Range(-30, 30)));
timer = 0.0;
}
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
and the code in C#, I only got one error in the line 24 where transform.position is:
using UnityEngine;
using System.Collections;
public class POLICEscript : MonoBehaviour {
private Quaternion qTo;
float speed = 1.5f;
float rotateSpeed = 3.0f;
public float timer = 0.0f;
void Start()
{
qTo = Quaternion.Euler (new Vector3 (0.0f, 0.0f, Random.Range (-30f, 30f)));
}
void Update()
{
timer += Time.deltaTime;
if(timer > 2)
{
qTo = Quaternion.Euler (new Vector3 (0.0f, 0.0f, Random.Range (-30f, 30f)));
timer = 0.0f;
//transform.position = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
transform.position = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
transform.Translate (Vector3.forward * speed * Time.deltaTime);
}
}
}
I will also appreciate if you help me to get rid of unnecessary parts
Show the code you have converted as well (so this doesn't just read as a 'convert-my-code' question.
Everything is practically the same, except of course for variable declarations; method prefixes; new keyword for class/structs.
private Quaternion qTo;
void Start()
something = new Quaternion .....
Here's some links I found useful in converting between C# and JS :
thanks you so much alucardj, I fixed some errors, some of them by simply adding a f after the float, I didn't know even if is 10,0 you have to add an f, but I still have an error left. I updated the question and added the code in C# as you told me, hope you can help me.
Answer by HarshadK · Sep 15, 2014 at 02:23 PM
Actually it is a very simple thing that you are missing.
On the line with the error you are setting the position as Quaternion where as it should be rotation like:
Current:
transform.position = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
Change to:
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
thank you so much Harshad$$anonymous$$, I'm still wondering how the code can work with transform.position in US, and with transform.rotation in C#, odd, but thank you so much!!! you da real $$anonymous$$VP!