I’ve looked around and can’t seem to find an answer that solves my problem, so I’ve decided to ask around.
I have a character (Spartan King free asset) that sits inside of an empty Game Object called “Player Character” on this Game Object I have a script called “Player Controller” which moves the character to follow the mouse. The “Player Character” Game Object also has a Character Controller attached. It’s fairly simple and works just fine. The problem I’m having is the character will not follow the terrain when moving. The “Player Character” Game Object already has a capsule collider attached as part of the character controller, but no rigidbody. The terrain has a terrain collider attached to it. What am I missing?
void Update()
{
if ( Input.GetMouseButton( 0 ) )
{
Plane playerPlane = new Plane( Vector3.up, player.transform.position );
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float hitDist = 0.0f;
if ( playerPlane.Raycast( ray, out hitDist ) )
{
Vector3 targetPoint = ray.GetPoint( hitDist );
Quaternion targetRotation = Quaternion.LookRotation( targetPoint - player.transform.position );
player.transform.rotation = Quaternion.Slerp( player.transform.rotation, targetRotation, speed * Time.deltaTime );
player.transform.position += player.transform.forward * speed * Time.deltaTime;
}
}
}
Maybe a rigidbody would help, also I think that you’ll do better using rigidbody forces to go up or down terrain. Without the rigidbody the engine might not be able to make the objetc go forward and top to acompain the terrain, I’m not sure, try adding a rigidbody and use forces instead of transform.forward.
Object.rigidbody.AddForce(transform.forward * 10)
that 10 is the strenghth
EDIT: don’t forget to use Time.deltaTime 
Once I attach a rigidbody, the character falls right through the terrain.
you can try add object’s RigidBody and BoxCollider or … but no use #MeshCollider.
if your Character use CharacterControler, you should not Rigidbody.
this is My idea.
private Vector3 moveDirection ;
if (Input.GetButtonDown ("Fire1"))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray,out hit))
{
PointerClick.position = hit.point;
}
}
Vector3 target = PointerClick.position ;
moveDirection = (target - transform.position);
if( moveDirection.magnitude < 1)
{
moveDirection = Vector3.zero;
}
rigidbody.velocity = moveDirection.normalized * speed;
This isn’t working. Perhaps I’m having a poor understanding of how the assets and components should be setup correctly. Currently I’m using a Character Controller and I can get the motion, but it won’t follow the contours of the terrain. So, should I be using a RigidBody? If so, I’ve been told by others that they don’t work with Character Controllers. So, which one should I be using and how should I apply movement? I tried using the Move() method on the Character Controller and that will follow the terrain up, but it will not follow it down. What is the proper combination of components to get the desired results?
This code worked. You can reference.
Character’s Components: CharacterController, Animation, file Script. Ok
Note: No add rigidbody components. Only use CharacterControler.(Read in: http://docs.unity3d.com/Documentation/Components/class-CharacterController.html)
using UnityEngine;
using System.Collections;
public class AIPerson : MonoBehaviour {
public bool loop;
public float speed = 6.0f;
public float gravity = 1000.0f;
public int _SpeedCharacter = 5;
public float _isForward = 0.2f;
public Vector3 getPositionTarget;
private Vector3 moveDirection ;
private Vector3 moveCharacter;
private Vector2 moveDirection2D;
public Transform PointerClick;
void Start () {
getPositionTarget = transform.position- new Vector3(0.01f,0.01f,0.01f);
}
void Awake()
{
getPositionTarget = transform.position- new Vector3(0.01f,0.01f,0.01f);
PointerClick = GameObject.Find("poiter").transform;
}
RaycastHit hit = new RaycastHit();
Ray ray = new Ray();
void Update () {
// Lay toa do cua chuot
if (Input.GetButtonDown ("Fire1"))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition); // Construct a ray from the current mouse coordinates
if (Physics.Raycast(ray,out hit))
{
Debug.DrawLine (Camera.main.transform.position, hit.point, Color.red);
getPositionTarget = hit.point;
PointerClick.position = hit.point;
}
}
// dieu khien nhan vat di chuyen
CharacterController controller = GetComponent<CharacterController>();
controller.detectCollisions = true;
if (controller.isGrounded) {
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
moveDirection = moveDirection - new Vector3(0,moveDirection.y+1.0f,0);
//print("cham dat");
}else
{
print("Khong cham");
}
//-------------------------------------------------------------
Vector3 target = getPositionTarget;
moveDirection = (target - transform.position);
moveDirection2D = new Vector2(target.x,target.z) - new Vector2(transform.position.x, transform.position.z);
// neu da toi noi thi dung
if(moveDirection2D.magnitude < 1)
{
moveDirection = Vector3.zero;
animation.CrossFade("idle");
PointerClick.gameObject.SetActive(false);
}else
{
animation.CrossFade("dibo");
PointerClick.gameObject.SetActive(true);
}
if( moveDirection.magnitude < 1)
{
moveDirection = Vector3.zero;
}
//rigidbody.velocity = veclocity;
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(_SpeedCharacter*moveDirection/ Vector3.Distance(target, transform.position) * Time.deltaTime);
transform.LookAt(new Vector3(getPositionTarget.x, transform.position.y ,getPositionTarget.z));
}
}