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 /
avatar image
0
Question by LightAxe2402 · Mar 27, 2018 at 10:38 PM · 2d game2d-platformer2d-physics

2D Collisions Not Working

I have a bullet object that I'm trying to get to collide with 2 different sets of objects. I separate the two types of objects using two different tags, one is moveable thus allowing the bullet to apply velocity to the collided object. The other is static like the ground. For some reason collisions with the moveable objects work but not the collisions with the static objects and I don't know why. I've made sure to attach 2d Box Colliders to all the objects. Any help would be greatly appreciated since I am still very new to unity. My bullet script is attached below.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bullet : MonoBehaviour {
 
     public Transform gunTransform;
     public float speed = 5f;
     public float lifeTime = 5f;
 
     Vector2 moveVector;
 
     public void Start()
     {
         gunTransform = GameObject.Find("PlayerGun").transform;
 
         moveVector = gunTransform.rotation * Vector2.right;
         moveVector /= moveVector.magnitude;
     }
 
     void FixedUpdate()
     {
         transform.Translate(moveVector * speed * Time.deltaTime);
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         Debug.Log("Colliding");

         //Not Working, Static objects don't even registor collisions
         if (collision.collider.tag == "StaticObstacle")
         {
             Debug.Log("Hit Static");
             Destroy(gameObject);
         }

         //Working Script
         if (collision.collider.tag == "MoveableObstacle")
         {
             collision.collider.GetComponent<Rigidbody2D>().velocity = moveVector * speed;
 
             Destroy(gameObject);
         }
     }
 
     void Update()
     {
         lifeTime -= Time.deltaTime;
 
         if (lifeTime <= 0)
         {
             Object.Destroy(gameObject);
         }
     }
 
 }

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TanselAltinel · May 09, 2018 at 01:17 PM

If your object with collider is moving, you need to attach a RigidBody to that object too.

It's RigidBody2D for your 2D colliders.

The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces. By adding the appropriate collider component, the sprite will also respond to collisions with other sprites. This behaviour comes entirely from Unity's physics system; very little code is required to get impressive and authentic physical behaviour and allows for "emergent" gameplay that was not explicitly coded into the game.

https://docs.unity3d.com/ScriptReference/Rigidbody2D.html

Comment
Add comment · 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
0

Answer by codemaker2015 · Nov 13, 2021 at 04:48 PM

Ensure the following things are considered in your code,

  1. All gameobject should contain collider attached and the player gameobject should contain the rigidbody component in it.

  2. The collider size should change to the width and height of the component instead of default (1,1) values.

  3. Uncheck isKinematic option in Rigidbody component if it's selected.

alt text


screenshot-386.png (42.4 kB)
Comment
Add comment · 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

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

91 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

Related Questions

I need to shoot a projectile in my 2D platformer in the opposite direction my player is dashing which can be in any direction the player points the joystick 2 Answers

Having some stuck issues on the 2D infinite runner 0 Answers

Unity crashes 0 Answers

HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers

Problem with 2D movment system C#. Keeps moving when no command is given 0 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