- Home /
Help with Rotating code?
Hi! I'm new to unity and i'm learning how to use C Sharp coding language, currently in my tech demo the player Holds a knife, (A child to the first person camera) and i'm making a script attached to the knife game object, in this script i want the player to use Left click on their mouse to "Rotate" the knife and simulate a attacking motion, here is the script i wrote,
void Update () {
if (Input.GetKey(KeyCode.Mouse0)) {
transform.Rotate(Vector3.right) = true;
} else {
transform.Rotate(Vector3.right) = false;
}
} }
The error with this script: Assets/My assets/Scripts/Knife_Hit.cs(9,14): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
What does this mean? could someone fix this script and tell me how its done, thanks. :)
Answer by MezFo · Oct 23, 2017 at 02:29 AM
@isaiastheweezerd It looks like you need to use a speed variable to tell it to move instead of true/false.
Try adding a float to you script called speed and write it like this (below).
public float speed = 1f;
void Update () {
if (Input.GetKey(KeyCode.Mouse0)) {
transform.Rotate(Vector3.right * speed);
} else {
transform.Rotate(Vector3.right * 0);
}
However you will probably want to look into slerping for a knife swing attack. With simple rotation it will be hard to get the knife back to it's original position when not holding down mouse. With slerp you can make it rotate towards position x while key is down and automatically rotate back to original position when key is up.
} else { transform.Rotate(Vector3.right * 0); }
Does nothing.
} else { transform.rotation = Quaternion.identity; }
maybe?
Answer by adipatil3517 · Oct 23, 2017 at 05:06 AM
void Update () { if (Input.GetKey(KeyCode.Mouse0)) { transform.Rotate(Vector3.right) ; } }