How do I instantiate my game object away from my transform?

I’m currently spawning my object at my players location. I want to spawn it a distance in the direction of the mouse.

	public GameObject shot;
	Transform playerPosition;

	Vector2 target;
	Vector2 targetVector;

	Transform shotPosition;

	void Start () {
		playerPosition = GetComponent<Transform> ();
	// Update is called once per frame
	void Update () {
		target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		// Target location - current location
		targetVector.x = target.x - transform.position.x;
		targetVector.y = target.y - transform.position.y;
		//shotPosition.position = new Vector2 (targetVector.normalized.x, targetVector.normalized.y);

		if (Input.GetMouseButton(0)){
			Instantiate (shot, shotPosition.position, Quaternion.identity);

I changed playerPosition to shotPosition in instantiate. The targetVector gives me the x and y values away from my playerPosition where shotPosition should be. How do I add my targetVector to my player position

Good day.

I think your code is wrong composed (maybe I’m wrong, but…) try replace this 2 lines, and add the distance. First, create a float or int variable called distance (to set the distance from player to spawning point, in my example i set to 50)

now replace the definition of shotPosition to this:

shotPosition.position = new Vector2 (targetVector.x, targetVector.y);
distance = 50;

and Instantiate

Instantiate (shot, shotPosition.position.normalized * distance, Quaternion.identity);

So shotPosition.position.normalized is the direction with magnitude 1. Thats why we need to * distance.

Bye! :smiley: