hi
i want some help in move my character to target point this code is doing it but when the target hit click is in high place my character fly and didn’t walk on ground
i want fix it or if any code better than my code or other method to do that
var speed =30 ;
var rotateSpeed = 3.0;
var target : Vector3;
var gravity = 20.0;
function Update ()
{
var vec : Vector3;
var hit : RaycastHit;
var targetdistance;
var forward;
var hitcube; // a cude appear at target point named target cube
var controller : CharacterController = GetComponent(CharacterController);
if (Input.GetButtonDown ("Fire1"))
{
// destroy target cube if founded
hitcube = GameObject.Find("targetcube");
DestroyImmediate (hitcube,true);
// get target by mouse fire1 button hit
var ray = camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray,hit) )
{
target = hit.point;
// look at target point
controller.transform.LookAt(target);
// create a target cube at target point
hitcube = GameObject.CreatePrimitive(PrimitiveType.Cube);
hitcube.name = "targetcube";
hitcube.transform.position = target;
}
}
// calculate distance
targetdistance = Vector3.Distance(controller.transform.position , target);
// check distance forr wlak or stop
if(targetdistance <= 0.05 )
{
// destroy target cube at stop position and set speed to zero
speed = 0;
hitcube = GameObject.Find("targetcube");
DestroyImmediate (hitcube,true);
}
else
{
// set speed to 30 and goto forward
speed =30;
forward = transform.TransformDirection(Vector3.forward);
controller.Move( forward * speed* Time.deltaTime);
}
}
@script RequireComponent(CharacterController)
thank you