I’m very new to Unity and C#. I’m trying to create my first game and I have now invested about 15+ hours trying to find solutions to this issue...I’m stumped!

My player sprite (blue square) needs to launch a prefab sprite (red circle) of similar size ‘appearing’ to originate from itself. Here is a GIF of what I have so far...I don’t want the blue cube to collide and go flying.

After clicking and dragging on Player (drag-to-aim) via OnMouseDrag to set the target vector, I’m trying to Instantiate a prefab sprite (Dynamic RigidBody2D, CircleCollider2D) using the Player sprite’s position (a Dynamic RigidBody2D, BoxCollider2D) and have it APPEAR to be originating from inside the Player object. Launch is triggered via function called in OnMouseUp. Both objects should immediately come under control of Physics to collide with the environment which will include other moving RigidBody2D objects later. Targeting should allow 360 degrees to include straight into an obstacle allowing rebound effect.

Here is some excerpts of my code:

using System.Collections;
using UnityEngine;

public class Teleportation_Grenade : MonoBehaviour {

    This script is attached to Player and requires_ 
    projectile prefab.

    tested with thrust set between 100 and 200.

    public float thrust;
    public Transform prefab;
    public LineRenderer playerShotLine;

    private Vector2 playerToMouse;
    private Vector2 targetVector;


    void OnMouseDrag()

        Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        Vector2 playerToMouse = mouseWorldPoint - transform.position;
        mouseWorldPoint.z = 0f;

        playerShotLine.SetPosition (0, transform.position);
        playerShotLine.SetPosition (1, mouseWorldPoint);
        playerShotLine.sortingOrder = 3;

        //opposite direction of line renderer
        targetVector = -playerToMouse;


    void OnMouseUp()

        SpawnPrefab ();

    void SpawnPrefab()

        Instantiate(prefab, new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity);

        prefab.GetComponent<Rigidbody2D> ().AddForce (targetVector * thrust, ForceMode2D.Impulse);

This is only one of many, many iterations. It's the gist of it though. I appreciate any insight from the community.

  • 2
    \$\begingroup\$ I didn't understand your layer's experiment. As far as I can understand from your this long detailed question that you just want to Instantiate Red Circle from Blue Square and Blue Square should not move, if is that so, why didn't you assign different layer to Blue Square and uncheck the collision from Collision Matrix? Edit -> Project Settings -> Physics2D \$\endgroup\$ Commented Mar 9, 2017 at 11:53
  • 2
    \$\begingroup\$ I've used both layer-based and IgnoreCollision methods successfully in the past, so it sounds like your problem may be in the implementation. Maybe you successfully ignored one colliding pair when one or both of your objects actually include multiple colliders, for example. To help debug your implementation, we'll need to see how you set it up, both in your code and in the inspector/hierarchy. \$\endgroup\$
    – DMGregory
    Commented Mar 9, 2017 at 15:10
  • 1
    \$\begingroup\$ The layers solution sounds like it would work. Perhaps you should take another look at that solution and maybe taking a look at the red square's physics? \$\endgroup\$
    – user84154
    Commented Mar 9, 2017 at 19:24
  • \$\begingroup\$ Yup! This was it indeed...layers had me all confused. Thanks! \$\endgroup\$ Commented Mar 10, 2017 at 8:40

1 Answer 1



As several comments pointed out, I was indeed misusing the Physics2D layer collision matrix and WAY, WAY overthinking the solution! As I pointed out, I'm very new to Unity so I didn't realize the extent to which you can manipulate layers.

Here's what I did:

  1. Created new layers (not "sorting layers") for each object type, background, platforms, player and projectile
  2. Updated my projectile prefab to use the "projectile" layer (drag into hierarchy > on object Inspector...change layer on object > Apply > Delete object from hierarchy)
  3. Assigned each object to it's appropriate layer (same as prefab but don't delete)
  4. Open the Inspector panel with the Physics2D Collision Matrix Physics 2D Settings (menu: Edit > Project Settings > Physics 2D)
  5. In the Inspector Collision Matrix, set all NEW layers to collide by placing a checkmark in the intersecting boxes, making sure that "Player" and "Projectile" layers would not collide (leave unchecked)
  6. Save scene, Play, test

This produces the desired affect!

  • 1
    \$\begingroup\$ Great to see you sorted it out! Make sure you come back and accept this answer so other users know it was the solution. \$\endgroup\$
    – Gnemlock
    Commented Mar 10, 2017 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .