Make object move with Bool C#
my goal is make my spike move Up and Down when the bool is checked. But if i run? no matter what, it turned to True, and start to going down!!! I didnt even checked it. Please help me...!
using UnityEngine; using System.Collections;
public class MovingSpike : MonoBehaviour
{
public GameObject spike;
public float moveSpeed;
public Transform spikeUp;
public Transform spikeDown;
public bool moving;
void Update ()
{
if (moving = false)
{
spike.transform.position = Vector3.MoveTowards (spike.transform.position, spikeUp.transform.position, Time.deltaTime * moveSpeed);
}
if (moving = true)
{
spike.transform.position = Vector3.MoveTowards (spike.transform.position, spikeDown.position, Time.deltaTime * moveSpeed);
}
}
}
@jgodfrey is correct. But even better would be
if (!moving)
{
spike.transform.position = Vector3.$$anonymous$$oveTowards (spike.transform.position, spikeUp.transform.position, Time.deltaTime * moveSpeed);
}
else
{
spike.transform.position = Vector3.$$anonymous$$oveTowards (spike.transform.position, spikeDown.position, Time.deltaTime * moveSpeed);
}
But it is still the same as @jgodfrey's answer.
Answer by jgodfrey · May 07, 2016 at 01:33 AM
At the very least, this...
if (moving = false)
and this...
if (moving = true)
Should be:
if (moving == false)
and...
if (moving == true)
oh my god.... i was trying to other way to solve this for a day.
By the way.. why it is "==" not "="
" = " is an assignment
" == " this is a comparison.
Using "=" assigns a a value to something. For instance:
int a = 1;
assigns the value "1" to the variable "a".
"==" tests for equality between two things. For instance:
if (a == 1)
tests if variable "a" is equal to the value of "1".
Assu$$anonymous$$g your question is answered, please accept the answer.
Your answer