Hi Guys, I’ve got the following script on a Plane (the floor) that makes my character snap to the hit point:
#pragma strict
var pokeForce : float;
var transform : Transform;
var Character : GameObject;
private var ray: Ray;
private var hit : RaycastHit;
function Start () {
}
function Update () {
if(Input.GetMouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.transform.name == "Floor"){
Character.transform.position = hit.point;
}
}
}
}
However, I want to improve it in two ways:
I want the Character to move smoothly between its current position and the hit point, not snap
I want the Character to face the hit point/ the direction its going in
Could anyone point me in the right direction please?
Try this, used 0.1 for Lerp time, change it as required to match your speed. This will give you some basic understanding of how Lerp works. For speed slowing down at end read above link answer where Eric5h5 explained why it happens and how can you avoid it. Good luck coding.
var pointToGo : Vector3 = Vector3.zero;
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
{
if(hit.transform.name == "NavBar")
{
Zombie.transform.LookAt(hit.point);
pointToGo = hit.point;
//Zombie.transform.position = hit.point;
ZombieAnim.animation.CrossFade("Walk Legacy", 0.5);
ZombieIdle.animation.CrossFadeQueued("Idle Legacy", 0.4, QueueMode.CompleteOthers);
}// if
}// if
}// if
Zombie.transform.position = Vector3.Lerp( Zombie.transform.position, pointToGo, 0.1);
}