- Home /
Smooth Rotation instead of LookAt in waypoint system
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);
}
}
}
Answer by SteveBC · Oct 31, 2014 at 11:17 AM
Calling lookAt in your Update function outside your if-statement should do the trick.
But to get a real smooth lookAt, try lerping the rotation. Take a look at jaxas' answer here, that seems to have done the deal. http://forum.unity3d.com/threads/smooth-look-at.26141/
It works partially. but character rotate to target smoothly additionally it's rotating y direction also... any suggestion...
Your answer
Follow this Question
Related Questions
Rotate towards movement direction 1 Answer
Asset Bundle question about properties being saved 0 Answers
Help with Character Controller 1 Answer
8 directional movement rotation problem 0 Answers