- Home /
Pathfinding
Hi guys, I've been following Burgzergs tutorials online and have been focusing on the enemy follow scripts. What I'm trying to achieve is once the 'player' hits the 'target' (Node A) position they will move to the next (Node B), and so on and so on up to at least 10 nodes.
using UnityEngine;
using System.Collections;
public class MoveTo : MonoBehaviour {
// private Transform myTransform;
public Transform target;
public float moveSpeed;
public int rotationSpeed;
public float jumpHeight;
public int jumpCount = 0;
private Transform myTransform;
void Awake()
{
myTransform = transform;
}
//public GameObject ObjectA;
//public GameObject ObjectB;
public float smooth = 5.0F;
void Start()
{
GameObject go = GameObject.FindGameObjectWithTag("PathNode");
target = go.transform;
}
// Update is called once per frame
void Update ()
{
//bool grounded;
if (target) {
float dist = Vector3.Distance(target.position, transform.position);
print("Distance to Node: " + dist);
Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//MoveTo towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
public static float RelativeTurnAngle(Transform turn, Vector3 target) {
// determine the angle
Vector3 relative = turn.InverseTransformPoint(target);
return Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
}
}
I have also tried to have a list system but cannot wrap my head around it.
I'm pretty much trying to get from one 'point' to another- If it makes it easier i'm working on a runner game and want the player to run a track on it's own but with controls. eg: move left and right with jump while going towards the next 'point'.
I found this script but I can't get the player to move and it's in java- i'm new to program$$anonymous$$g and still have issues converting to C#.
var target : Transform[];
var timer : float = 0.0;
var rate : float = 5.0;
private var currentTarget: int;
function FixedUpdate () {
timer += Time.deltaTime;
if(timer > rate){
this.GetComponent("Player").StartPath(transform.position,target[currentTarget].position);
timer = 0;
}
if(Vector3.Distance(transform.position,target[currentTarget].position)<=0){
currentTarget++;
if(currentTarget>=target.Length){
currentTarget = 0;
}
Okay first I in your first script i think i didn't see anywhere you defining wether the player is close to a path node or not
Secondly
In your second script I believe StartPath is another function and not a built in fearure
So here is the conversion
//Omitted Using UnityEngine and others
public Transform[] target;
public float timer = 0.0f;
public float rate = 5.0f;//you should use f at the end of num in C#
Transform _transform;
int currentTarget;
void Awake(){
_transform = transform;//Cache it for performance
}
void FixedUpdate(){
timer += Time.deltaTime;
if(timer > rate){
_transform.GetComponent<Player>().StartPath(_transform.position,target[currentTarget].position);
timer = 0;
}
if(Vector3.Distance(_transform.position,target[currentTarget].position)<=0){
currentTarget++;
if(currentTarget>=target.Length){
currentTarget = 0;
}
}
}
Cheers for this, but is there a way to move the player here without using the 'StartPath'?
Also should _transform = Transform; be _transform = transform? 'cos it come up with an error :(