- Home /
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);
}
}
}
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);
}
}
}
A note! Don't forget to remove the ! from the if statement in the update function!
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);
}
Sorry, but nothing is happening? The debug isn't even being called. And there are no errors.
Okay, the debug is showing now. But nothing is instantiating.
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
![](https://koobas.hobune.stream/wayback/20220612103027im_/https://answers.unity.com/themes/thub/images/avi.jpg)