• 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 _minirlz · Dec 20, 2016 at 07:51 AM · gameobjectonmousedown

Limit where I can Instantiate on mousedown?

My script puts an object at the position of the mouse, regardless of the position.

I'd like to limit where I can place the object to a small area around the player sprite.

Say if mouse X if greater than (X position + a little) of the players X than the object wont Instantiate. I've tried if statements along these lines but haven't been able to get it to work.

Here is the placement script.

 public GameObject seedlings;
 public GameObject player;
 Vector3 mousePOS = Input.mousePosition;

 
 // Use this for initialization
 void Start(){}

 // Update is called once per frame
 void Update()
 {
     PlantInGround();
 }



 void PlantInGround()
 {
     Vector3 mousePOS = Input.mousePosition;
   

         if (Input.GetMouseButtonDown(0))
         {
             mousePOS.z = +12;
             mousePOS = Camera.main.ScreenToWorldPoint(mousePOS);
             Instantiate(seedlings, (mousePOS), Quaternion.identity);
             Debug.Log(mousePOS);
         }
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by _minirlz · Dec 21, 2016 at 07:37 AM

Okay so for whatever reason GrKls code did not work for me. So I took it and changed the if statement and it worked exactly how I wanted it to. Thanks for the help man. Leaving this hear for others.

public class PlantItem : MonoBehaviour {

 public GameObject seedlings;
 public GameObject player;
 Vector3 mousePOS = Input.mousePosition;

 void Update()
 {
     if (!Input.GetMouseButtonDown(0))
     {
         PlantInGround();
     }
 }

 void PlantInGround()
 {
     Vector3 mousePOS = Input.mousePosition;

     mousePOS.z = +12;
     mousePOS = Camera.main.ScreenToWorldPoint(mousePOS);
     if (((player.transform.position.y < mousePOS.y + 0.5) && (player.transform.position.y > mousePOS.y - 1.5)) && ((player.transform.position.x < mousePOS.x + 1) && (player.transform.position.x > mousePOS.x - 1)))
     {
         Instantiate(seedlings, mousePOS, Quaternion.identity);
      
     }
 }

}

Comment
Add comment · Show 2 · 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 _minirlz · Dec 21, 2016 at 07:14 AM 0
Share

A note! Don't forget to remove the ! from the if statement in the update function!

avatar image GrKl · Dec 21, 2016 at 07:40 AM 0
Share

you just needed to replace "x" by "y".

avatar image
1

Answer by GrKl · Dec 20, 2016 at 10:48 AM

This should do the job. Limit if X and Z distances between player and mousePOS are smaller than 2.

I moved your input check to Update, so you don't call "PlantInGround" at all if you did not click.

I also commented a line in your code that I don't understand, where you "mousePOS .z += 12". Why is this before the following line? Your setting mousePOS.z then change completely mousePOS on the next line...

 public GameObject seedlings;
 public GameObject player;
 Vector3 mousePOS = Input.mousePosition;
 
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         PlantInGround();
     }
 }
 
 void PlantInGround()
 {
     Vector3 mousePOS = Input.mousePosition;
     
     //mousePOS.z = +12;
     mousePOS = Camera.main.ScreenToWorldPoint(mousePOS);
     if ((Mathf.Abs(player.transform.position.x - mousePOS.x) < 2f) && (Mathf.Abs(player.transform.position.z - mousePOS.z) < 2f))
     {
         Instantiate(seedlings, mousePOS, Quaternion.identity);
     }
     Debug.Log(mousePOS);
 }

Comment
Add comment · Show 3 · 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 _minirlz · Dec 21, 2016 at 06:11 AM 0
Share

Sorry, but nothing is happening? The debug isn't even being called. And there are no errors.

avatar image _minirlz _minirlz · Dec 21, 2016 at 06:13 AM 0
Share

Okay, the debug is showing now. But nothing is instantiating.

avatar image _minirlz · Dec 21, 2016 at 06:24 AM 0
Share

Okay, so it is instantiating now. But not properly. I can instantiate anywhere in a diagonal direction. I can't instantiate anywhere in a direct line from the player. Which is mostly great but I want to be able to instantiate close to him only. Thanks for the help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference creator of gameObject 1 Answer

Change Mesh/Gameobject onmousedown?,Swap mesh/gameobject randomly onmousedown 0 Answers

Detecting name through OnMouseDown 1 Answer

Only Allow One Click on GameObject? 2 Answers

How can I move a player toward a gameobject by clickling an image 0 Answers


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