Other.
Help with changing position of my waypoint.
Hello all, novice question for you. Im making a small pizza delivery game. Currently trying to get a Delivery Waypoint system in order. Someone earlier showed me the list function and I figured its perfect for what Id like to happen. The way I envision it is there are all the vector3 positions preselected on the map. I need one of them to fire off as the currentMarker and have the Gameobject I selected in the inspector (Red Waypoint Box) to be moved to the new currentMarker. Ive looked and searched but cant seem to write it in a way that unity understands whats the hell I want from it. Here is the script Im working with. Thanks in advance.
public class PositionFinder : MonoBehaviour { //Hold a list of all possible positions public List markerPositions = new List(); public GameObject Waypoint;
//The active marker we are chasing
public Vector3 currentMarker = Vector3.zero;
//Call this function to start or when the user reaches a marker
public void OnMarkerComplete()
{
HELP HERE // Waypoint.transform.position = new Vector3.currentMarker;
//If we have markers remaining in our list we can find a new one
if (markerPositions.Count > 0)
{
//pick a random index between min and max index
int randomIndex = UnityEngine.Random.Range(0, markerPositions.Count - 1);
//Assign the random marker we found as the active one
currentMarker = markerPositions[randomIndex];
//Remove the marker we are using from the list
markerPositions.RemoveAt(randomIndex);
}
}
}
Answer by Saryk360 · Jun 11, 2019 at 12:41 AM
Try something like this :
public List<Vector3> markerPositions = new List<Vector3>();
public GameObject waypoint;
public Vector3 currentMarker = new Vector3();
public void OnMarkerComplete()
{
//If we have markers remaining in our list we can find a new one
if (markerPositions.Count > 0)
{
//pick a random index between min and max index
int randomIndex = UnityEngine.Random.Range(0, markerPositions.Count - 1);
//Assign the random marker we found as the active one
currentMarker = markerPositions[randomIndex];
// set the waypoint to the randomly selected marker's position
waypoint.transform.position = currentMarker;
//Remove the marker we are using from the list
markerPositions.RemoveAt(randomIndex);
}
}
Now that I notice it, Its not giving me a change in my current$$anonymous$$arker in the Inspector. So Im guessing its not moving the "Waypoint" to the position because its sitting at 0.0.0? but in that case it should at least move the object to 0,0,0 right? nothing moves at all. no errors.
Can I see the declaration for markerPositions ?
Sorry, not sure why it got cut off at the top.
public List markerPositions = new List(); I then wen into the Inspector window and manually added Vector3 positions. in the inspector currently its $$anonymous$$arker Positions: Size 3 Element 0: X48,Y-3,Z30 Element1:X17,Y-3,Z30 Element2:X18,Y-3,Z-3
did I provide the right info?
Also I edited the declaration of current$$anonymous$$arker.
yeah haha sorry let me ask a question a step back. I feel like Im missing a crucial part. I have a vector 3 list at the very top called marker Positions. $$anonymous$$y goal was to take one of the Vector3 positions at random and move the "Destination" object to it. once there, rinse and repeat. I have the vector 3 list to choose from. The choosing at random part doesn't seem to work. Should I post the script in full again? it got cut off at the top for some reason.
Answer by NoodleCup · Jun 11, 2019 at 01:24 AM
public class PositionFinder : MonoBehaviour
{
//Hold a list of all possible positions - public makes it show in the inspector so your can set them there
public List<Vector3> markerPositions = new List<Vector3>();
// markerPositions.Add(new Vector3(48f,-3f,31f));
public GameObject Waypoint;
public Vector3 currentMarker = new Vector3();
public void OnMarkerComplete()
{
//If we have markers remaining in our list we can find a new one
if (markerPositions.Count > 0)
{
//pick a random index between min and max index
int randomIndex = UnityEngine.Random.Range(0, markerPositions.Count - 1);
//Assign the random marker we found as the active one
currentMarker = markerPositions[randomIndex];
// set the waypoint to the randomly selected marker's position
Waypoint.transform.position = currentMarker;
//Remove the marker we are using from the list
markerPositions.RemoveAt(randomIndex);
}
}
}
This is with the "changes" you've suggested to make.
What triggers On$$anonymous$$arkerComplete()
?
What happens or does not happen now ?
(you should change Waypoint
to waypoint
also)
currently the On$$anonymous$$arkerComplete() when I pick up a pizza. I have a pizza counter with a max of 3. right now its on a pizza object. you drive over it. Pizza counter goes to 1. and uses that function. I assumed it would start a waypoint when player grabs a pizza to be delivered.
looks like I moved it to the Start, was trying to get the script to kicka a new Vector3 for the "current$$anonymous$$arker" since I can see it in the inspector while the game is running. No luck. It might be in the wrong place for all I know.
heres the script for that.
public class PickUpPizza : $$anonymous$$onoBehaviour
{
public void Start()
{
GetComponent<PositionFinder>().On$$anonymous$$arkerComplete();
}
void OnTriggerEnter(Collider other)
{
if(other.name == "Car01")
{
other.GetComponent<Pizza$$anonymous$$anager>().pizzaCount++;
Destroy(gameObject);
}
}
}
Why are you calling On$$anonymous$$arkerComplete()
in Start()
?
Shouldn't it be when the pizza is picked up ?