My purpose:
When player click on the screen, we check the x and y coordinates of the point he click on, and then move a 2D sprite toward that place.
My idea is, when mousePosition.x < transform.position.x, we move sprites left.
For example: when ( mousePosition.x < transform.position.x AND mousePosition.y < transform.position.y ), we move character toward direction 2.
And that makes sense to me.
But I get “error CS0019: Operator ‘<’ cannot be applied to operands of type ‘float’ and ‘UnityEngine.Vector3’.” message all the time, so I’m wondering if there’s any solution or better way to do this.
You most likely have a typo in your code. Since you didn’t posted your actual line of code we can’t help you any further besides some speculations.
If the compiler tells you that you try to compare a float value to a Vector3 value, then you actually doing this. You either don’t have a “.x” / “.y” behind the Vector3 value or it’s otherwise syntactically wromg.
Sample Codes!
//Variables
private Vector3 mousePosition;
void Update() {
if ( Input.GetMouseButton(0) )
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if ( //Your Condition, eg. mousePosition.x < transform.position.x )
{
//Do something...
//eg. transform.position = new Vector3(-1,0,0);
}
else if ( //Your Condition )
{
//Do something...
}
}
}