If then statment not working with bool

Updated on August 3, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on August 3, 2018

So I have a bool called isMoving in my Camera Control script and I have an if then statement in a update function in my boat Controller script that says

if (CameraController.isMoving)

{

Move();

}

and

void Move()

{

float forwardForce = Mathf.Clamp(Input.GetAxisRaw(“Vertical”), 0f, 1f);

rb.AddForce(transform.forward * forwardForce * speed);

float turn = Input.GetAxisRaw(“Horizontal”);

rb.AddTorque(transform.up * turn * turnSpeed);

}

and even when isMoving is true, it wont work!  Can You please help.  Also is there a better way to post my code on this website?

 

  • Liked by
Reply
0 on August 3, 2018

Can we seen the entire script?

and yes, there’s a button all the way to the right that looks like a square with some numbers in it. That is how you insert code.

  • Liked by
Reply
Cancel
2 on August 3, 2018

Here is the code and I couldn’t see a square with some numbers.

Code for BoatController –>

public float speed = 5f;

public float turnSpeed = 2f;

private Rigidbody rb;

public CameraController CameraController;

void Start ()

{

rb = GetComponent<Rigidbody>();

CameraController = GetComponent<CameraController>();

}

void Update ()

{

if (BuildingManager.isBuidling)

{

transform.rotation = Quaternion.AngleAxis(0, Vector3.up);

}

if (!BuildingManager.isBuidling)

{

Move();

}

}

void Move()

{

float forwardForce = Mathf.Clamp(Input.GetAxisRaw(“Vertical”), 0f, 1f);

rb.AddForce(transform.forward * forwardForce * speed);

float turn = Input.GetAxisRaw(“Horizontal”);

rb.AddTorque(transform.up * turn * turnSpeed);

}

 

Code for CameraController –>

 

public float panSpeed = 10f;

public Vector2 panLimit;

public bool isMoving;

public Vector3 offset;

public Transform target;

public float smoothSpeed = 0.125f;

public void SetIsMovingTrue()

{

isMoving = true;

}

void LateUpdate()

{

if (isMoving)

{

transform.position = target.position + offset;

}

}

void Update ()

{

Vector3 pos = transform.position;

 

if (!isMoving)

{

if (Input.GetKey(“w”))

{

pos.z += panSpeed * Time.deltaTime;

}

if (Input.GetKey(“a”))

{

pos.x -= panSpeed * Time.deltaTime;

}

if (Input.GetKey(“s”))

{

pos.z -= panSpeed * Time.deltaTime;

}

if (Input.GetKey(“d”))

{

pos.x += panSpeed * Time.deltaTime;

}

}

pos.x = Mathf.Clamp(pos.x, -panLimit.x, panLimit.x);

pos.z = Mathf.Clamp(pos.z, -panLimit.y, panLimit.y);

 

transform.position = pos;

}

 

on August 3, 2018

try renaming the CameraController variable so it’s not the exact same as the class.

on August 3, 2018
Show more replies
  • Liked by
Reply
Cancel