Z
Z
Zimaell2020-05-07 14:33:16
C++ / C#
Zimaell, 2020-05-07 14:33:16

How to properly use conditions in C#?

I am writing a test case in Unity, here is a piece

public class MovePlayer : MonoBehaviour{
  public Transform player;
  void OnMouseDrag(){
    Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    mousePos.x = mousePos.x > 3f ? 3f : mousePos.x;
    /*--------*/
    if (mousePos.x>3f) mousePos.x=3f; else mousePos.x;
               /*--------*/
    player.position = new Vector2 (mousePos.x, player.position.y);
    }
  }

mousePos.x = mousePos.x > 3f ? 3f : mousePos.x;

works
if (mousePos.x>3f) mousePos.x=3f; else mousePos.x;

does not work,
although in theory these conditions are identical ...
Tell me what's wrong, just switched from PHP, and the syntax seems to be the same ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dyupachev, 2020-05-07
@Zimaell

And why are there generally yes the same conditions? The second condition does not make sense after the first, where everything has already been checked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question