Instantiate and launch prefab that's overlapping player?
I’m very new to Unity and C# and I’m trying to create my first game. I've 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 Launch_Projectile : 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.
Fixed...Here's what I did:
Created new layers (not "sorting layers") for each object type, background, platforms, player and projectile
Updated my projectile prefab to use the "projectile" layer (drag into hierarchy > on object Inspector...change layer on object > Apply > Delete object from hierarchy)
Assigned each object to it's appropriate layer (same as prefab but don't delete) Open the Inspector panel with the Physics2D Collision $$anonymous$$atrix Physics 2D Settings (menu: Edit > Project Settings > Physics 2D)
In the Inspector Collision $$anonymous$$atrix, 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)
Save scene, Play, test
This produces the desired affect!
Answer by MelvMay · Mar 09, 2017 at 12:17 PM
Very hazy on what the problem is here.
Create a Layer for your "Player" and a layer for your "Projectile". Go to 2D Physics setting and in the Layer Collision Matrix, uncheck the box that controls "Player" v "Projectile" collisions. That way, they won't ever collide with each other addressing the issue of "I don’t want the blue cube to collide and go flying."
You can also create a layer of "Ground" and have your scenary set to that if you like.
Spot on! I was way, way over thinking this...I didn't realize the Layer Collision $$anonymous$$atrix was as useful as it is. I was also confusing sorting layers with the regular layers...sorry for the long winded question...thank you!
No problem, glad that was all it was. Have fun!