transform.Translate alternative issues

Alright, I’m working on a simple tank movement script and came across this. It works just fine until I run into a wall, or rather, don’t run into it. I know transform.Translate doesn’t take have a collision detection system, and so I’m looking for a workable alternative. Problem is, every possibility I’ve found sends up an error. Usually that it exist in the context, so I keep looking for other ways.

using UnityEngine;

using System.Collections;

public class Player : MonoBehaviour {

    public float movementSpeed = 10;
    public float turningSpeed = 60;
    void Update() {
        float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
        transform.Rotate(0, horizontal, 0);    
        float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
        transform.Translate(0, 0, vertical); 
	}	
}

What if you would use a box collider, a rigidbody and wheel colliders? You could even reproduce the tank movement simply by applying motor torque to both wheels on one side.

–

Thats a possibility. The main issue is that there is a very picky mesh that doesn't always want to cooperate, so I'll give this a shot and see where it gets me.

–

So the box collider works, and but the rigidbody tends to cause the tank to slide away after a collision, like its on ice. It also forces me to only turn left for some reason.

–

Give it a large mass. And maybe you could constraint its movements. But first give it a 3000 mass or more.

–

Nah. It's mass isn't the issue.

–

1 Answer

1

I’m just going to post my own answer in case someone comes across this down the road. In order to fix the skating issue, I upped the angular drag and the drag portions of the Rigidbody. So now there is friction, and running into something results in actual collision.