After trying long time I got this way point system to walk the character in defined path under user control but now the problem is while turning from one way point to another there is no smooth rotation. It looking the target suddenly.
How can I get smooth rotation while moving from one point to another instead of rotation done at waypoint itself then character is walking.
I tried lot but not worked . . . .
Here is my code
public class wayCSpoint : MonoBehaviour {
public Transform player;
public Transform[] waypoint;
public float speed = 5f;
public bool loop = true;
private Animator anim;
private CharacterController character;
private int CurrentwayPoint = 0;
private Vector3 lastPos,delta,target,moveDirection;
private float H,vel;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
character = GetComponent<CharacterController> ();
lastPos = transform.position;
}
// Update is called once per frame
void Update () {
H = Input.GetAxis ("Horizontal");
delta = transform.position - lastPos;
lastPos = transform.position;
delta.y = 0;
vel = delta.magnitude / Time.deltaTime;
if (H != 0) {
if (vel < 0.2)
{
anim.SetBool("idle",false);
anim.SetBool("walk",true);
}
if (CurrentwayPoint < waypoint.Length){
target = waypoint[CurrentwayPoint].position;
target.y = transform.position.y;
moveDirection = target - transform.position;
if(moveDirection.magnitude < 0.5f)
{
//transform.position = target; // force character to waypoint position
CurrentwayPoint++;
}
else
{
transform.LookAt(target); // Here I have to change But not worked that what I tried
character.Move(moveDirection.normalized * speed * Time.deltaTime);
}
}else{
if(loop)
{
CurrentwayPoint=0;
}
}
}else{
anim.SetBool("walk",false);
anim.SetBool("idle",true);
}
}
}