- Home /
Get GameObject to face and move towards Raycast hit point
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: 1. I want the Character to move smoothly between its current position and the hit point, not snap 2. I want the Character to face the hit point/ the direction its going in
Could anyone point me in the right direction please?
Here are some suggestions
Use Vector3.Lerp to move your character smoothly within a given time from point a to point b
Use Character.transform.LookAt( hit.point ); for looking at a point.
Educate yourself by googling above suggestions
I've fixed the LookAt(hit.point), thanks for the suggestion Sundar. I've had a fiddle with the Vector3.Lerp and that still seems to either nudge the GameObject in the direction of the hit point, or make it jump to the hit point. Any suggestions on how I could fix it? Here's the script I tried out:
#pragma strict
var transform : Transform;
var Zombie : GameObject;
private var ray: Ray;
private var hit : RaycastHit;
// Transforms to act as start and end markers for the journey.
var start$$anonymous$$arker: Transform;
var end$$anonymous$$arker: Transform;
// $$anonymous$$ovement speed in units/sec.
var speed = 1.0;
// Time when the movement started.
private var startTime: float;
// Total distance between the markers.
private var journeyLength: float;
var target : Transform;
var smooth = 5.0;
function Start () {
// $$anonymous$$eep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
journeyLength = Vector3.Distance(start$$anonymous$$arker.position, end$$anonymous$$arker.position);
}
function Update () {
// Distance moved = time * speed.
var distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
var fracJourney = distCovered / journeyLength;
if(Input.Get$$anonymous$$ouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.transform.name == "NavBar"){
//Zombie.transform.LookAt(hit.point);
// Set our position as a fraction of the distance between the markers.
Zombie.transform.position = Vector3.Lerp(start$$anonymous$$arker.position, end$$anonymous$$arker.position, fracJourney);
}
}
}
}
As I said you need to understand how Lerp works here is a best answer http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html You can clearly see you are calling Lerp inside an if condition which is called only once hence your object is jumping to the destination. Go through above example answer, if you still having problem I will fix your code.
I get the idea of it, it's just implementing it that I'm having trouble with. I'm fairly new to coding and struggle with these more complex code functions. If you could give me a hand with this code I can go through it thoroughly and pick it to bits. Thank you!
$$anonymous$$y current code is as follows:
#pragma strict
private var ZombieAnim : GameObject;
private var ZombieIdle : GameObject;
var transform : Transform;
var Zombie : GameObject;
private var ray: Ray;
private var hit : RaycastHit;
function Start () {
ZombieAnim = GameObject.Find("ZF_idle");
ZombieIdle = GameObject.Find("ZF_idle");
}
function Update () {
if(Input.Get$$anonymous$$ouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.transform.name == "NavBar"){
Zombie.transform.LookAt(hit.point);
Zombie.transform.position = hit.point;
ZombieAnim.animation.CrossFade("Walk Legacy", 0.5);
ZombieIdle.animation.CrossFadeQueued("Idle Legacy", 0.4, Queue$$anonymous$$ode.CompleteOthers);
}
}
}
}
Answer by Sundar · Jan 30, 2014 at 12:43 PM
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);
}