This question was
closed Feb 15, 2018 at 07:03 AM by
Koblih161 for the following reason:
The question is answered, right answer was accepted
Question by
Koblih161 · Feb 10, 2018 at 07:33 PM ·
vector3accelerometer
What did i do wrong? ":" is wrong expression?,What did I do wrong? ":" is wrong expression
void FixedUpdate () {
var speed = 10.0;
var dir : Vector3 = Vector3.zero;
dir.x =-Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir*= Time.deltaTime;
transform.Translate(dir * speed);
}
Comment
Are you using C# or JavaScript? JavaScript syntax for declaring variables is different than C#'s.
Best Answer
Answer by Meguia · Feb 10, 2018 at 07:58 PM
Tip: Use variable types.
Tip: Don't multiply Vector3 * Double.
Pro tip: use {} even if it's a line ;-)
Try this:
void FixedUpdate () {
float speed = 10.0F;
Vector3 dir = Vector3.zero;
dir.x =-Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1){
dir.Normalize();
}
dir*= Time.deltaTime;
transform.Translate(dir * speed);
}
Answer by Pigenator · Feb 10, 2018 at 07:47 PM
Well i'd imagine your use of : in "var dir : Vector3 = Vector3.zero" is wrong, but I don't use unityscript so i don't know how it's supposed to be.
Not necessarily. Unity prefers explicit types like int, float, bool, etc. Var doesn't really have a place in UnityScript, as far as I'm concerned. It's more used in JavaScript.