C# - Using Transform to move ignores Colliders?

So I’m new to scripting and was using the code below to move my player character which is a cube.

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
	public float movementSpeed = 25;
	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);
	}
	
}

However with a Box Collider assigned to both my cube and another cube which is stretched to form a wall, the box passes through it.

I can assign a rigidbody to the player cube and it will not pass through the wall cube however physics takes over and the player cube bounces off it or can climb up it when continuing to walk into it.

I could tweak the values to prevent drag and such, but I’m sure their is a more simpler way to prevent the player cube from passing through the wall cube. Searched for responses on here but didn’t have any luck, any ideas?

Try and use Character Controller Overview and Scripting

You add the Component Character Controller to you box, and remove the Box Collider. Character Controller have its own collider.

Character Controller is located at: Component → Physics → Character Controller

You use it like this:


using UnityEngine;
using System.Collections;
 
public class Player : MonoBehaviour {
    public float movementSpeed = 25;
    public float turningSpeed = 60;
 
   private CharacterController characterController;
    // Use this for initialization
    void Start()
    {
        this.characterController = GetComponent< CharacterController>();
        
    }
    void Update() {
        float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
        transform.Rotate(0, horizontal, 0);

        float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
        Vector3 movement = new Vector3(0, 0, vertical);
        this.characterController.Move(movement);
    }
}

You have a few options. You can use a character controller. Or you can attach a kinematic ridgidbody to the character. Also kill gravity on the body and you should have functional collision without physics effects.