- Home /
Move to closest point in front of player with raycast
I have my player, When he lands on the rail I turn on isKinematic and lerp his transform.position to the next point. Now what I would like to do, Is make it so he moves towards the closest point in front of him or the direction he is going. I thought this would be easy, but im finding this extremely difficult.
This is what I've thought of so far :
create a raycast in front of the player
point the raycast to the closest point with tag "point"
move player towards raycast direction
This is what I have so far
if( OnRail == true ){
gameObject.GetComponent(Rigidbody).isKinematic = true;
transform.position = Vector3.MoveTowards(transform.position, nearRailPoint.transform.position,Time.deltaTime * force / 3);
}
I did try to come up with raycast code but nothings working. I am using javascript ( Yes i know, Im awful at learning code ), any help is appreciated thank you!
Your question is bit confusing.
Are you facing problem with the raycast code or the moveTowards code? How do you find nearRailPoint ?
are the "Point" located in front of the player ?
Basically,When my player jumps onto the rail it will move along the rail in the direction its facing, I am trying to do this by getting the closest way point on the rail and setting the players transform.position to lerp to that waypoint on the rail. Does that make sense?
$$anonymous$$aybe debug if the nearRailPoint
is the point what you need, you can create a simple static class on a C# script
public static class HelperSpawn{
public static void Add$$anonymous$$arker(Vector3 pos, Quaternion rot)
{
GameObject parent = new GameObject("$$anonymous$$arker");
parent.transform.position = pos;
parent.transform.rotation = rot;
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.SetParent(parent.transform);
sphere.transform.localPosition = Vector3.zero;
sphere.transform.localScale = new Vector3 (1.5f, .1f, 1.5f);
GameObject prism = GameObject.CreatePrimitive(PrimitiveType.Cube);
prism.transform.SetParent (parent.transform);
prism.transform.localPosition = Vector3.up;
prism.transform.localRotation = Quaternion.Euler (36f, 45, -45f);
parent.transform.localScale = new Vector3 (.5f, 1f, .5f);
$$anonymous$$aterial transparent$$anonymous$$aterial = new $$anonymous$$aterial (Shader.Find ("Standard"));
transparent$$anonymous$$aterial.color = new Color (0f, 0.635294f, 1f, 0.482353f);
sphere.GetComponent<$$anonymous$$eshRenderer>().material = transparent$$anonymous$$aterial;
prism.GetComponent<$$anonymous$$eshRenderer>().material = transparent$$anonymous$$aterial;
}
}
this will spawn a (fancy?) marker, used to easy debug positions
you can add the following code just after assign nearRailPoint
HelperSpawn.Add$$anonymous$$arker (nearRailPoint.transform.position, Quaternion.identity);
to know if the raycast is pointing to the correct objetive, also, maybe force is very little and the player don't do a visible transition , multiply force by a great number (`1000` is enough maybe) to see if the position of the player change
EDIT: I dont know if this really would help, probably all your scripts are in js, so you can develop a similar marker there or use a prefab, or some like
Debug.DrawLine((nearRailPoint.transform.position, transform.position)
the goal is discover if nearRailPoint
is storing the position that you want to go
Wow thanks for the reply, Thats alot to take in! I will give this a go tonight when i get back from work, I just apply this script to the player correct?
Thank you in advance!