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;
}
}
}