- Home /
The question is answered, right answer was accepted
click and move vihicle on terrain
Hi,
I have a tank game and want to make the controll for mobile like a click on the terrain and move the tank there. The tank has 3 axes and Wheelcolliders.
I already tried several things but allways run in the problem with different terrain heights. As long as the terrain is flat it is no problem to move the tank, but if there is a small or then the tank get's a strange behavior.
IEnumerator MoveToPosition()
{
yield return null;
while(Vector3.Distance(transform.position, target) > 3 ) {
if(Vector3.Distance(transform.position, target)>3)
{
transform.LookAt(target);
Debug.Log (Vector3.Distance(transform.position, target));
}
rb.MovePosition(transform.position + transform.forward * 10 * Time.deltaTime);
yield return null;
}
MoveToTarget= false;
yield return null;
}
What do you mean by "strange beahviour"? How is your "target" variable filled with a position? What does $$anonymous$$ovePosition do exactly? If rb is a rigidbody like your tag implies you should use forces ins$$anonymous$$d of changing the position directly. Why do you return null at the beginning of the method? You should read the api documentation of components you use carfully. $$anonymous$$ost components have a little questionmark at the top right. Click it.
the strange behavior is that the tank don't use the physics anymore, it's not rotating in the vertical axis if the tank drives a hill. After the click it makes a look at to the target and then don't changes the rotation anymore.
the return null if a relict of a further testing.
I get the target position with a raycast and a hit.point.
$$anonymous$$ovePosition is simply not a good approach for what you are trying to do. Have you thought about using Nav$$anonymous$$esh, AddForce, or a more robust wheel system to drive the tank ins$$anonymous$$d?
I already tried a navmesh, but it also had to problem with the tank rotations. I think I will try it with addForce.
Have you enabled any of the constraints on the tank's Rigidbody?
Answer by n1gth · Jul 07, 2015 at 06:38 AM
Try making putting the NavMeshAgent on the parent object, and the model of the tank in the child object. Then use the NavMeshAgent to drive the tank, and in the Update method of the child, rotate the tank to match the terrain.
See this thread for more discussion and sample code.
I had not so much luck with adding force. I will try the Nav$$anonymous$$eshAgent script from your link. thanks.
ok I tried the script of your link and it works like charm for me. Thank you.
Follow this Question
Related Questions
Make a simple tree 1 Answer
Create a terrain prefab? Saving a terrain? No? 1 Answer
Terrain trees 1 Answer
Generate 16bit terrain map from real world elevation data 3 Answers
Array index is out of range? 0 Answers