movement script help

Hi,

I’ve created a simple script for movement which uses transform to move the character. The problem I’m having is that when I add rigidbody, the character starts moving through the terrain. Without rigidbody, it moves in the direction I want without any difficulty. Is rigidbody the way to go for adding gravity and collision detection or is there a better way to do this?

using UnityEngine;
using System.Collections;

public class ActionControl : MonoBehaviour {

	public AnimationClip idleAnimation;
	public AnimationClip runAnimation;
	public AnimationClip walkAnimation;
	
	private bool isControllable = true;
	private Vector3 DesiredLocation;
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
	
		if (!isControllable)
		{
			// kill all inputs if not controllable.
			Input.ResetInputAxes();
		}
		
		if( Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0 )
		{
			
			float x = Input.GetAxis("Horizontal");
			float z = Input.GetAxis("Vertical");
			
			DesiredLocation = new Vector3( transform.position.x + x, transform.position.y, transform.position.z + z );
			transform.position = DesiredLocation;
		}
		
	}
}

check out Character Controller:

otherwise, to stop falling thru the floor, give it a box/sphere/capsule… collider as well as a rigidbody,

I have a capsule collider. I am using the standard worker asset which comes with unity. but it still goes through the ground so I’m not sure why.

uncheck is trigger from the collider

use Rigidbody.AddForce and/or Rigidbody.AddTorque to make your rigidbody move or set a constantForce. and make sure the collider on your box/plane has IsTrigger unchecked.

here’s some reading for you: Unity - Scripting API: Rigidbody

the standard worker asset has a character controller?

Rigidbody Character controller toguether are very unstable. I have tryed it months ago.

I used then only Rigidbody and i was programming the movements with forces.

thanks. That’s most likely where it going wrong then. If that’s the case, is it better to stick with rigidbody or to go with character controller?

It is easyer with character controller, but you are more “physics free” with a rigidbody. And more realistic finish.