- Home /
Gameobject follow between array of gameobjects
Hello guys. I have a doubt in my code that doesn't work. I'm trying to get my cube follow the path between an array of gameobjects with a variable to control the speed of the cube and jump when it is requested. If anyone can help!.My code:
void Update()
{
target.position = new Vector3(path[c].position.x, path[c].position.y + 1f, path[c].transform.position.z);
if (target.position.z >= path[c].position.z)
{
if (c < path.Length)
c++;
}
target.LookAt(path[c]);
if (Input.GetKey(KeyCode.UpArrow))
{
rigidbody2D.AddForce(target.forward * speed2);
}
}
Answer by IvovdMarel · Feb 05, 2015 at 12:31 AM
If you want to make it stop when it's close to the target, you can use Vector3.Distance (and check if it's smaller than a certain amount).
If you call AddForce every frame, more and more force will be applied and the object will most likely accelerate really fast. Instead, directly set the velocity by using rigidbod2D.velocity.
$$anonymous$$y problem is that I have an array of 242 gameobjects. $$anonymous$$y cube until moves between the path but it passes very, very fast. I want to control this speed
As I said: directly set the velocity ins$$anonymous$$d of using AddForce
AddForce is only called when the button is pressed. I say in the constant movement of the cube that is too fast. He goes through each node very fast. I've used velocity in Update() but it didn't work
So isn't the idea that you hold the key to make the object move?
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
{
rigidbody2D.velocity = target.forward * speed2;
} else {
rigidbody2D.velocity = Vector3.zero;
}
Sorry if that didn't come out right. The cube will move infinitely, but it's movement is too fast. This snippet: if (Input. Get$$anonymous$$ey ($$anonymous$$eyCode. UpArrow)) is a leap that the cube only makes. So, it's not my problem
Your answer
Follow this Question
Related Questions
2D gameobject array coordinates 1 Answer
Instantiating a gameObject in an array is not working for me 1 Answer
2D Array of GameObjects... 1 Answer
2D array of GameObjects C# 2 Answers
2D GameObject Array to 2D List 1 Answer