Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Roguespectre101 · Oct 30, 2012 at 07:22 AM · mousespawnclickswitchlocation

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 :).

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Roguespectre101 · Oct 30, 2012 at 06:05 PM 0
Share

Right here is a second and possibly more accurate version of what I need help with. I have a 3d game world that requires 4 separate spawn points that the player can get to using there mouse click. so 4 different spawn points in the same scene you will not be able to see the other spawn points because there is are walls/game objects blocking you from seeing the other spawn points. So basically there are 4 different spawn points and I need to be able to mouse click/mouse down to each of the different spawn points so can't I use something like on$$anonymous$$ouseClick change location/position and just enter the other 3 locations well that's what I'm guessing I could do anyway but I don't know how to put that into code so yeah. I also need to be able to take all of my current player characteristics with me when I spawn so the time and score settings are not allowed to change either. If this is a more accurate questions then please answer away because I'm pretty stumped at how to do this at the moment. Thanks for your responses so far :).

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Rosonator · Oct 30, 2012 at 08:35 AM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by AlvinHerawan · Oct 30, 2012 at 11:53 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Roguespectre101 · Oct 31, 2012 at 01:09 AM 0
Share

Right here is a second and possibly more accurate version of what I need help with. I have a 3d game world that requires 4 separate spawn points that the player can get to using there mouse click. so 4 different spawn points in the same scene you will not be able to see the other spawn points because there is are walls/game objects blocking you from seeing the other spawn points. So basically there are 4 different spawn points and I need to be able to mouse click/mouse down to each of the different spawn points so can't I use something like on$$anonymous$$ouseClick change location/position and just enter the other 3 locations well that's what I'm guessing I could do anyway but I don't know how to put that into code so yeah. I also need to be able to take all of my current player characteristics with me when I spawn so the time and score settings are not allowed to change either. If this is a more accurate questions then please answer away because I'm pretty stumped at how to do this at the moment. Thanks for your responses so far :).

avatar image
0

Answer by jajskadu · Oct 31, 2012 at 12:45 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Rosonator · Oct 31, 2012 at 10:36 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse Click and Spawn object 4 Answers

Spawn Objects Where i click 2 Answers

How to prevent GameObject from spawning on top of each other? 1 Answer

Unity doesn't recognize mouse clicks, C# 1 Answer

How can I make the character move to clicked position without allowing the user to drag? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges