Im struggling with very weird and propably simple problem,I have created a robot and his task is to move towards target I mean Im using Vector3.MoveTowards function in void update to move my robot per every frame but the problem is that he is moving only one time and stopping, he is making one step instead of for example 100. Im working with Unity3D. Here is source code;
public class Test01 : MonoBehaviour
{
public float speed, stopDist, rotationSpeed, moveSpeed, minSpeed, maxBackSpeed, maxFrontSpeed, turnSpeed, riseSpeed;
public Transform target;
private Rigidbody rb;
private float currentSpeed;
public bool isFinding = false;
private PlaySound signal;
void Start()
{
signal = GameObject.FindGameObjectWithTag("Signal").GetComponent<PlaySound>();
rb = GetComponent<Rigidbody>();
}
private void Update()
{
// If space key button is pressed the robot's isFinding bool is becaming true and robot is starting searching for target.
if (Input.GetKeyDown(KeyCode.Space))
{
isFinding = true;
if (Vector3.Distance(transform.position, target.position) > stopDist)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
signal.isAlarming = true;
signal.Sound();
}
else if (Vector3.Distance(transform.position, target.position) < stopDist)
{
isFinding = false;
signal.isAlarming = false;
SoundManager.instance.StopSound();
StopChasing();
}
}
}
public void StopChasing()
{
transform.position = this.transform.position;
}
CodePudding user response:
Well currently you have everything nested under the GetKeyDown
so it is executed only exactly once.
You could change that doing
if (Input.GetKeyDown(KeyCode.Space))
{
isFinding = true;
}
if(isFinding)
{
if (Vector3.Distance(transform.position, target.position) > stopDist)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
signal.isAlarming = true;
signal.Sound();
}
else
{
isFinding = false;
signal.isAlarming = false;
SoundManager.instance.StopSound();
StopChasing();
}
}
So pressing Space
once activates the isFinding
mode until it arrives at Vector3.Distance(transform.position, target.position) < stopDist
CodePudding user response:
Input.GetKeyDown
is a function right when you pressed down the key.
You might want to change that to Input.GetKey
instead, so it will move everytime you hold space.
Like this,
private void Update()
{
// If space key button is pressed the robot's isFinding bool is becaming true and robot is starting searching for target.
if (Input.GetKey(KeyCode.Space))
{
isFinding = true;
if (Vector3.Distance(transform.position, target.position) > stopDist)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed* Time.deltaTime);
signal.isAlarming = true;
signal.Sound();
}
else if (Vector3.Distance(transform.position, target.position) < stopDist)
{
isFinding = false;
signal.isAlarming = false;
SoundManager.instance.StopSound();
StopChasing();
}
}
}
If you take the Input.GetKeyDown
for the robot to start targetting. Then split it like this
private void Update()
{
// If space key button is pressed the robot's isFinding bool is becaming true and robot is starting searching for target.
if (Input.GetKeyDown(KeyCode.Space))
{
isFinding = true;
}
if (isFinding)
{
if (Vector3.Distance(transform.position, target.position) > stopDist)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed* Time.deltaTime);
signal.isAlarming = true;
signal.Sound();
}
else if (Vector3.Distance(transform.position, target.position) < stopDist)
{
isFinding = false;
signal.isAlarming = false;
SoundManager.instance.StopSound();
StopChasing();
}
}
}
After all, what you're doing before doesn't really use the isFinding
as anything, you'll be just setting it as true and it will do nothing to affect the code.