system
1
Hi there, and thanks for your attention.
Im (very) new to c# and im trying to write my first script that moves a character.
The script activates on a keycode, and when it has a certain y rotation, it will move.
Otherwise, it will rotate towards this y rotation.
A part of my script goes like this:
public void move_Left(){
if(transform.rotation == Quaternion(transform.rotation.x,270,transform.rotation.z,transform.rotation.w)){
transform.position = new Vector3(transform.position.x - 5,transform.position.y,transform.position.z);
} else {
transform.rotation = new Quaternion(0,270,0,transform.rotation.w);
}
}`
The problem is that i get an error that says :
“error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected”
How can I fix this ??
thanks
The quaternion components x, y and z have nothing to do with the rotation angles around axes x, y and z - you should use transform.eulerAngles instead. Another point: it’s better to allow a small error margin to accept the 270 degrees angle; you probably could never rotate the object manually to a precise 270 degrees rotation, and anyway comparing floats hardly produces an equality.
float error = 1.5f; // accept the angle if inside this error margin
public void move_Left(){
float angleY = transform.eulerAngles.y;
if (angleY < 0) angleY = 360 + angleY; // y may come as -90 instead of 270
if (Mathf.Abs(angleY - 270) < error){ // accept values inside +/- range
transform.position = transform.position + new Vector3(-5, 0, 0);
} else {
transform.rotation = Quaternion.Euler(0, 270, 0);
}
}`
You forgot the new keyword in your if statement.
if (transform.rotation == new Quaternion(transform.rotation.x, 270, transform.rotation.z, transform.rotation.w))