U
U
Udee Mo2021-05-16 14:36:06
PHP
Udee Mo, 2021-05-16 14:36:06

How to call functions in ternary operator?

I am creating a control on one joystick, for two different objects. The X axis is responsible for one object, and the Z axis for another. To make them move in the right direction, I use ternary operators. Here is part of the code:

public float speed;
private float moveInput; 
private Rigidbody rb;
public Joystick j;

private void Start()
{
    rb = GetComponent<Rigidbody>();
}

private void FixedUpdate()
{
   if (j.Horizontal < 0)
   {
        j.Vertical < 0 ? ((j.Vertical < j.Horizontal) ? moveVertical() : moveHorizontal()) : ((j.Vertical > -j.Horizontal) ? moveVertical() : moveHorizontal());
   }
   else
   {
        j.Vertical < 0 ? ((-j.Vertical > j.Horizontal) ? moveVertical() : moveHorizontal()) : ((j.Vertical > j.Horizontal) ? moveVertical() : moveHorizontal());
   }
}
void moveHorizontal()
{
     moveInput = -j.Horizontal;
     rb.velocity = new Vector3(moveInput * speed, rb.velocity.y, rb.velocity.z);
}    
void moveVertical()
{
     moveInput = -j.Vertical;
     rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, moveInput * speed);
}

But the unit throws an error:
60a100c9c9f9c652861402.png
I know that these ternary operators need to be assigned, but since I'm using function references, I can't figure out what to assign them to.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita Ermilichev, 2018-11-26
@xonar

what fields are there in sh_refers_refers table? Is there a priority field in it? Can it be Null?
The fact is that you need to initialize all fields that do not have a default value or set them to this value

J
JohnnyMnemonik, 2018-11-27
@JohnnyMnemonik

Who will filter the variables? Any self-respecting student will hack your site at once. Don't be lazy and learn PDO

R
Rsa97, 2021-05-16
@UdeeMo

Don't use ternary operators unless you really need to. The usual if/else will suffice for you here.
PS In general, your bunch of conditions fit into

if (Math.Abs(j.Vertical) > Math.Abs(j.Horizontal)) {
    moveVertical();
} else {
    moveHorizontal();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question