- Home /
The question is answered, right answer was accepted
Flying Tank problem
Hi,
I made a simple tank 3D model and a simple controller script for it:
public class TankScript : MonoBehaviour {
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void FixedUpdate() {
float translation = Input.GetAxis("Vertical") * speed *-1;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(translation, 0, 0);
transform.Rotate(0, rotation, 0);
}
}
I gave the tank model a rigidbody and placed the tank on my terrain. My problem is, that the tank is starting to fly if it is driving over a small hill. It jumps and flys a long distance.
I tried to give it a really high mass, but that doesn't help. So, what can I do to avoid that problem?
thanks in advance Frank
this might be a dumb question but is gravity turned on for the rigidbody component?
yes gravity is turned on. the tank is moving on the ground, but if i drive up a small hill it's behavior is like a jetski on a wave.
im not sure, your code looks like it should work but try this change transform.Translate(translation, 0, 0); to transform.Translate(Vector3.Forward * translation, 0, 0);
also, maybe try making the gravity stronger? go to edit > project settings > physics > and make the gravity a bigger number
the gravity default setting is the earth gravity. i think that shouldn't be changed. perhaps it will be better to use .addForce on the ridgidbody ins$$anonymous$$d of transform.translate.
Answer by RLin · Jun 16, 2015 at 01:15 AM
You should really use rigidbody.addforce on the tank instead of modifying its transform. Anything with a non-kinematic rigidbody doesn't work well with non-instantaneous transform changes. Note that you may have to increase addforce to some pretty high values and then play around with it to get the right speed. Also, your tank will still be able to fly if it's going too fast, but this will be physically realistic.
I changed now to translate and the behavior seems to be better now, but it's speeding up to much, so I have to check out how to limit the force that I give to the object.
In the first person controller script I saw also that they set .velocity to the object to nail it to the ground?!
I changed now my script completely and get some good result with this:
using UnityEngine;
using System.Collections;
public class TankController : $$anonymous$$onoBehaviour {
private Rigidbody rb;
public float BaseSpeed = 80f;
public float BaseRotation = 120f;
public float $$anonymous$$axThrust = 6f;
public float $$anonymous$$axRotation = 4f;
private float thrust;
private float rotation;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
thrust = Input.GetAxis("Vertical");
rotation = Input.GetAxis("Horizontal");
if (rb.velocity.magnitude > $$anonymous$$axThrust)
{
rb.velocity = rb.velocity.normalized * $$anonymous$$axThrust;
}
if(rb.angularVelocity.magnitude > $$anonymous$$axRotation)
{
rb.angularVelocity = rb.angularVelocity.normalized * $$anonymous$$axRotation;
}
rb.AddForce(transform.forward * BaseSpeed * thrust);
rb.AddTorque(transform.up * BaseRotation * rotation);
}
void OnGUI()
{
GUI.Label(new Rect(100f, 10f, 500f, 50f), "Speed: " + rb.velocity.magnitude);
}
}
hmm it's damn hard to find the right mass / speed settings. at the moment I have the problem, that the tank tilt if I drive against the smallest hill.
$$anonymous$$any vehicles use wheelcolliders or other methods to move rather than addforce.
I think wheelcolliders is the next, what I will test. But I have to modify my tank before that. I never planned to make the wheels turn ^^
Follow this Question
Related Questions
Animating Tank Treads 0 Answers
fire tank in any direction 1 Answer
How to use 2 Character Controllers in a scene 1 Answer
player on trigger enter dont work? 1 Answer
Movement Issue: x value of object resets automatically 0 Answers