Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by aetherghost · Mar 09, 2017 at 10:51 AM · physics2dcollision detectioncollider2dinstantiate prefab

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aetherghost · Mar 10, 2017 at 08:56 AM 0
Share

Fixed...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) Open the Inspector panel with the Physics2D Collision $$anonymous$$atrix Physics 2D Settings (menu: Edit > Project Settings > Physics 2D)

  4. 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)

  5. Save scene, Play, test

This produces the desired affect!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aetherghost · Mar 10, 2017 at 08:54 AM 0
Share

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!

avatar image MelvMay ♦♦ aetherghost · Mar 10, 2017 at 10:27 AM 0
Share

No problem, glad that was all it was. Have fun!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hit from kinematic rigidbody2d 1 Answer

Physics 2D with tile collider corner problem 2 Answers

How To create 2D collision detection on object which changes dimensions 1 Answer

Collision detection 1 Answer

Unity Platform Effector 2D doesn't work for me 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges