About 'Spawn At Mouse Position'!

In this video (objectives)...

  1. Use ScreenToWorldPoint to identify where the mouse is clicked in relation to our play space.
  2. Investigate the relationship between methods with return types and methods with arguments.

After watching (learning outcomes)...

Instantiate a defender where the mouse pointer is currently located.

(Unique Video Reference: 21_GL_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

1 Like

So I solved the challenge by plugging my GetSquareClicked() method directly into the instantiation within SpawnDefender(). This is my code below

public class DefenderSpawner : MonoBehaviour
{

    [SerializeField] GameObject defender;

    private void OnMouseDown()
    {
        SpawnDefender();
    }

    private Vector2 GetSquareClicked()
    {
        Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
        return worldPos;
    }

    private void SpawnDefender()
    {
        GameObject newDefender = Instantiate(defender, GetSquareClicked(), Quaternion.identity) as GameObject;
    }

}

but instead in the course, we passed an argument through our SpawnDefender() method and then put GetSquareClicked() within SpawnDefender() when we call it in OnMouseDown()

public class DefenderSpawner : MonoBehaviour
{

    [SerializeField] GameObject defender;

    private void OnMouseDown()
    {
        SpawnDefender(GetSquareClicked());
    }

    private Vector2 GetSquareClicked()
    {
        Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
        return worldPos;
    }

    private void SpawnDefender(Vector2 worldPos)
    {
        GameObject newDefender = Instantiate(defender, worldPos, Quaternion.identity) as GameObject;
    }

}

Both approaches solve the problem of getting the defenders to spawn on the mouse position. What is the advantage to creating an argument within SpawnDefender() as oppose to my technique of just plugging GetSquareClicked() directly into the Instantiation? Is one approach more efficient in terms of code? Perhaps more clear to other coders in a collaborative project? Or is creating an argument better for scalability of larger projects?

Hi Rick,

Defender Always Spawns at Bottom Corner.

I tried to use a solution by another person by enabling the Apply Root Motion for the Cactus Prefab, but that didn’t work.
Here is my script for DefenderSpawner.cs. Thank you in advance for your help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DefenderSpawner : MonoBehaviour
{

[SerializeField] GameObject defender;



private void OnMouseDown()
{
    Debug.Log("mouse was clicked:");
    SpawnDefender(GetSquarClicked());
    
}

private Vector2 GetSquarClicked() 
{
    Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    /* Since we are using the grid system created in the game as 1 world unit
       You need to convert the pixel position into a world unit with the following:*/
    Vector2 worldPos = Camera.main.ScreenToViewportPoint(clickPos); 
    return worldPos;
    
}
private void SpawnDefender(Vector2 worldPos)
{
    
    // as GameObject puts the game object in the hierarchy and manipulate it and other things
    GameObject newDefender =
        Instantiate(defender, worldPos, Quaternion.identity) as GameObject; 
}

}

Hi @Rick_Davidson,

I have the same question as Vincent. What is the advantage of using kind of “inception” way rather than plugging GetSquaredClicked method into transform.position. It looks much more simple and easy.
Would really appreciate an explanation on both ways.

Thanks

Privacy & Terms