Mouse click changes spawn location how do you do it?

Hey guys need a little help I’m currently doing a uni project in unity and the project has asked us to create a game area which has several different spawn position and you can switch to each different spawn location using mouse click. So basically I need a script that will allow me to switch spawn position and still have all the same settings at each of the spawn location. I’ve got an idea on how to do it but tbh I’m not sure how to start it so can anyone suggest on how to do it. so yeah please help because I’m running out of time and I’ve tried to find a solution to doing this script but so far come up with nothing as of yet. So yeah need help asap :).

I’d use an empty game object just to contain all the logic. The script should have an onMouseDown() method in which you should throw a Raycast to the GO you are pointing with your mouse, with this:

void onMouseDown() {

RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

.
.
.
}

Then, still inside that method, you must get the hit to see if you have “touch” anything with the ray. Then, if we have succeed, we’ll save the game object in a variable;

if(Physics.Raycast(ray, out hit, 100))  //100 is the distance
   {
     private GameOBject spawnGO = hit.gameObject;
   }

So now you already know where to spawn your new game object, the only thing you need to do is

Instantiate (theGOname, spawnGO.position, new Vector3 (0,0,0)); 

Here you can do as you want. Maybe you need to spawn higher than the spawnGO so you should do as this spawnGO.position + new Vector3 (0,5,0), or maybe you could want to use the spawns rotation… as you desires.

I hope this serves to you, good luck!

This can be achieved in various ways. This also depends on whether your game environment is a 2D, 2.5D or 3D.

You mentioned that you have had an idea on how to do it. Maybe you could explain that a little bit more specific so that we could help you more effectively.

As Roso had mentioned, you use the GetMouseButtonDown to cast a beam of ray and hit an object that has a collider component. You then want to get the position of the object hit and move your spawning position to that particular position.

Simply instantiating will create multiple spawning points instead rather than moving existing spawn point. I suggest you update the spawn’s position rather than instantiating a new one.

Did a simple code like this to do the trick

var spawnLocation : Transform;

//This will be the object that spawns your creep.
//In this example, it will be moved around the spawn locations.
//I use particle to show this spawn pool.
var spawnPool : GameObject;

function Start () {

}

function Update () {

	if (spawnLocation == null)
	Debug.Log("No Spawn Point Selected");
	
	if (Input.GetMouseButtonDown(0)){
	
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		
		if (Physics.Raycast (ray, hit, 1000)) {
   			Debug.Log (hit.collider.name + " selected.");
   			spawnLocation = hit.collider.transform;
		}
		else{
			spawnLocation = null;
		}
	}

	spawnPool.transform.position = spawnLocation.position;
	

}

If it still doesn’t work, please download this package here http://db.tt/1skY3p8W and import it to your project.

Hope these help.

Mouse tap oxidized mouse good repair bad change it
There is said can be solved with DoubleClickFix but not used in the end use does not not know

Well, I think I understood you fine from the very beginning, so my answers is still mostly the same. I’ll put some order to it:

You could put all your spawn points into a tag, for example “spawnPoints”, so you don’t need anything about any other spawnPoint, only about the one you are clicking. Lets see what to put into code:

public GameObject theSpawnedGO; //Set the thing you want to instantiate.
private GameObject activatedSP;   
.
.
.

void onMouseDown() {

   RaycastHit hit = new RaycastHit();
   Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   if(Physics.Raycast(ray, out hit, 100)) {  //100 is the distance   
      if (hit.gameObject.tag == "spawnPoint"){ //If the thing you are touching is an Spawn Point
         activatedSP = hit.gameObject; //Set a new spawn point
      }
   }   
}

public void Spawn () {    //Call this function when you want to make a single spawn

   Instantiate (theSpawnedGO, activatedSP.position, new Vector3 (0,0,0));  //(0,0,0) or whatever
}

I hope you finally understand how to get you properly solution. Best wishes.