error CS0103: The name `dist' does not exist in the current context
hi guys trying to get the distance between one objetc and the leader, and make it follow the leader but have a set "GAP" between them, trying to find the distance and ajust it to the set GAP so my objects following the leader are evenly spaced out like in space shooters.
using UnityEngine;
using System.Collections;
public class Follow : MonoBehaviour {
public Transform Leader;
public float GAP = 10;
public float speed = 10;
public float GapSmooth = 5f;
// Use this for initialization
void Start () {
// get distance:
float dist = Vector3.Distance(Leader.position, transform.position);
}
// Update is called once per frame
void Update () {
transform.LookAt (Leader);
transform.Translate(Vector3.forward * speed * Time.deltaTime);
// GAP:
if(dist < GAP && dist > GAP){dist = GAP * GapSmooth *Time.deltaTime;}
}
}
FAQ :
Some reasons for getting a post rejected:
Asking us to fix your code: more than likely, you simply need to get a better understanding of basic program$$anonymous$$g. Having a look at the Scripting tutorials on the Learn page will help you become more familiar with scripting in Unity
Posting about a specific compiling error or NullReferenceException: there is a myriad of these questions with answers already, have a look at those posts to get hints on what could possibly be your issue
Answer by saschandroid · Mar 11, 2016 at 07:49 AM
You made dist
a local variable only known in the Start()
function. Change to:
public float GapSmooth = 5f;
float dist;
// Use this for initialization
void Start () {
// get distance:
dist = Vector3.Distance(Leader.position, transform.position);
}
Your answer
Follow this Question
Related Questions
Rotation script isn't working 1 Answer
How does CollisionFlags work? 0 Answers
[SOLVED]My OnMuseDown execute only once 1 Answer
First person shooter : Leaning! 2 Answers