Jump to content

Custom Random Spawnpoints.


SzbZsmbr

Recommended Posts

Hi guys, i'm working on a new callout pack and i made like everything but one thing, that i make custom vector3s where the callout will happen.

I have my coordinates, but i can't figure it out. I wanted to make like an array of some sort with all of the vector3s in it but didn't work. I was googleing for a while but i found nothing.

Is there any simple or not simple method that i can do?

So my goal is to get a random vector3 from a before specified list/array for the spawnpoint of the callout. 

Thanks in advance!

SzbZsmbr

Link to comment
Share on other sites

Would be suggested to have an extension:

public static class EnumerableExtension
{
	public static T PickRandom<T>(this IEnumerable<T> source)
	{
		return source.PickRandom(1).Single();
	}

	public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)
	{
		return source.Shuffle().Take(count);
	}

	public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
	{
		return source.OrderBy(x => Guid.NewGuid());
	}
}
      
      //Then you can do something like
      Vector3[] spawnPoints = ...
      Vector3 point = spawnPoints.PickRandom();

 

Link to comment
Share on other sites

17 hours ago, epicmrjuan said:

            Random rand = new Random();

            Vector3[] spawnpoints = new Vector3[]
            {
                new Vector3(0,0,0),
                new Vector3(0,1,0),
                new Vector3(1,0,1)
            };

            Vector3 spawnpoint = spawnpoints[rand.Next(0, spawnpoints.Length)];

Where spawnpoint becomes a random vecter3 from the array.

Thank you very very very much, i was sooooo close, ridiculous how a simple change in the line makes everything to not work! THANK YOU!

 

 

13 hours ago, OJdoesIt said:

Would be suggested to have an extension:


public static class EnumerableExtension
{
	public static T PickRandom<T>(this IEnumerable<T> source)
	{
		return source.PickRandom(1).Single();
	}

	public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)
	{
		return source.Shuffle().Take(count);
	}

	public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
	{
		return source.OrderBy(x => Guid.NewGuid());
	}
}
      
      //Then you can do something like
      Vector3[] spawnPoints = ...
      Vector3 point = spawnPoints.PickRandom();

 

Thanks for yours too, but it seems a bit chinese for me, i will study it.

SzbZsmbr

Link to comment
Share on other sites

23 hours ago, epicmrjuan said:

            Random rand = new Random();

            Vector3[] spawnpoints = new Vector3[]
            {
                new Vector3(0,0,0),
                new Vector3(0,1,0),
                new Vector3(1,0,1)
            };

            Vector3 spawnpoint = spawnpoints[rand.Next(0, spawnpoints.Length)];

Where spawnpoint becomes a random vecter3 from the array.

And how can i choose the closest one to Game.LocalPlayer.Character? I can't figure it out how can i get each vector from the array and compare them with the position of my character.

Thanks in advance!

SzbZsmbr

Link to comment
Share on other sites

6 hours ago, SzbZsmbr said:

Thank you very very very much, i was sooooo close, ridiculous how a simple change in the line makes everything to not work! THANK YOU!

 

 

Thanks for yours too, but it seems a bit chinese for me, i will study it.

The EnumerableExtension class just needs to be added somewhere within your project. You don't need to edit it or anything.

 

43 minutes ago, SzbZsmbr said:

And how can i choose the closest one to Game.LocalPlayer.Character? I can't figure it out how can i get each vector from the array and compare them with the position of my character.

Thanks in advance!

You would add the LINQ namespace via using System.Linq;

Then

Vector3 closestspawnpoint = spawnpoints.OrderBy(x => x.DistanceTo(playerposition)).First();

 

Link to comment
Share on other sites

1 hour ago, OJdoesIt said:

The EnumerableExtension class just needs to be added somewhere within your project. You don't need to edit it or anything.

 

You would add the LINQ namespace via using System.Linq;

Then


Vector3 closestspawnpoint = spawnpoints.OrderBy(x => x.DistanceTo(playerposition)).First();

 

Thank you so much!

SzbZsmbr

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...