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 $$anonymous$$ · Oct 17, 2017 at 12:09 AM · unity 5colliderunity 2dcollision detectioncollider2d

Platform Collider Not Working When Enemy Collides With It

So, I have this infinite runner / platformer game in which I generate an enemy on a platform. The platforms have 2 enemy colliders that is supposes to collide with the enemy, but my enemies just go past the colliders. My 2 colliders (left and right collider) are a child of my platforms. My platforms are generate using a script that is attached to my first platform.

Enemy.cs Script

 using UnityEngine;
 
 public class Enemy : MonoBehaviour {
 
     private SpriteRenderer sp;
 
     private float disMax;
     private float disMin;
     private float dis; // Distance travel / frame
 
     private void Start() {
 
         sp = GetComponent<SpriteRenderer>();
         
         PickDis();
         Flip();
 
     }
 
     private void Update() {
 
         Move();
 
     }
 
     /// <summary>
     /// Makes enemy move
     /// </summary>
 
     private void Move() {
 
         float x = transform.position.x + dis * Time.deltaTime;
         transform.position = new Vector3(x, transform.position.y, transform.position.z);
 
     }
 
     /// <summary>
     /// Picks a random distance / frame
     /// </summary>
 
     private void PickDis() {
 
         disMin = -1f;
         disMax = 1f;
 
         float rand = 0f;
 
         while(rand == 0f) {
             rand = Random.Range(disMin, disMax);
         }
 
         dis = rand;
 
     }
 
     private void Flip() {
 
         if (dis > 0f) {
             // Right
             sp.flipX = true;
         } else if (dis < 0f) {
             // Left
             sp.flipX = false;
         }
 
     }
 
 }

Platform Generator Script

 using UnityEngine;
 
 public class GeneratePlatforms : MonoBehaviour {
 
     public static GeneratePlatforms Instance = null;
 
     [SerializeField] private GameObject platform;
 
     private SpriteRenderer sp;
 
     private Camera cam; // Main camera
     private float camHeight; // Height of the camera view port
     private Vector2 currentPos; // Last platforms position
 
     private int maxPlatforms; // Maximum number of platforms
 
     // Min and max height range for generating platforms
     private float yMin;
     private float yMax;
 
     // Min and max scale range for width
     private float scaleWMin;
     private float scaleWMax;
 
     private float gap; // Gap between each platform
     private float w; // Width of the platform object
 
     private void Start() {
 
         sp = GetComponent<SpriteRenderer>();
         
         cam = Camera.main;
         camHeight = 2 * cam.orthographicSize;
         currentPos = transform.position;
 
         maxPlatforms = 20;
         
         yMin = (-camHeight / 2) + 0.8f;
         yMax = (camHeight / 2) - 3f;
 
         scaleWMin = 0.05f;
         scaleWMax = 0.14f;
 
         gap = 7f;
         w = sp.bounds.size.x;
 
         if (Instance == null) {
             Instance = this;
         } else if (Instance != this) {
             Destroy(gameObject);
         }
 
         Generate(maxPlatforms);
 
     }
 
     /// <summary>
     /// Generates max number of platforms on start
     /// </summary>
     /// <param name="max">Max number of platforms to generate</param>
 
     public void Generate(int max) {
 
         for (int i = 0; i < max; i++) {
             float randomY = Random.Range(yMin, yMax);
             float randomScaleW = Random.Range(scaleWMin, scaleWMax);
 
             // Up
             if (randomY > currentPos.y + 3f) {
                 gap = 10f;
             }
 
             // Down
             if (randomY < currentPos.y - 4f) {
                 gap = 5f;
             }
 
             float x = (currentPos.x + w) + gap;
             Vector2 lastPos = new Vector2(x, randomY);
 
             GameObject newPlatform = Instantiate(platform, lastPos, Quaternion.identity);
 
             newPlatform.transform.localScale = new Vector3(randomScaleW, newPlatform.transform.localScale.y, newPlatform.transform.localScale.z);
 
             currentPos = lastPos;
         }
 
     }
 
 }
 

Images

Collider

Enemy

capture.png (28.2 kB)
1.png (46.1 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Oct 17, 2017 at 12:20 AM

I figured it out, it turns out I needed to add a rigidbody to one gameobject.

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

152 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 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

How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers

How to destroy a gameobject's clone when colliding with the ground? 1 Answer

OnTriggerEnter2D being called by GameObject without the trigger 1 Answer

Trigger Triggers Without Collision 1 Answer

Help with bombs destroying colliders in different position from gameobject 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