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 Kk1496 · Jan 11, 2017 at 02:00 AM · 2dcollisionphysics

Early Collision Detection

I a making a game similar o the Space shooter example project. I've noticed that the targets (Red circles) collision detection seems to be happening earlier than it's supposed to. alt text The issue occurs when this script is active using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class ShipController : MonoBehaviour {
 
     private const float X_BOUNDARY = 5;                        //Clamp the ship's position
     private const float DOUBLE_SHOT_DELAY = 1.5f;            //Delay factor when spawning two shots
 
     [SerializeField] private float speed;                    //Ship Movement speed
 
     [SerializeField] private GameObject shot;                //Shot to instantiate
     [SerializeField] private Transform leftShotSpawn;        //Instantiate on left
     [SerializeField] private Transform rightShotSpawn;        //Instatiat on right
     [SerializeField] private float fireRate;                //Delay in seconds between shots fired
     private float nextFire;                                    //Shot counter
 
     private Rigidbody2D rb;                                    //Ship movement
 
     // Use this for initialization
     void Start () {
 
         rb = gameObject.GetComponent<Rigidbody2D> ();
     }
     
     // Update is called once per frame
     void Update () {
 
         //If both the fire buttons are pressed and the fireRate interval has passed
         if (Input.GetKey (KeyCode.J) && Input.GetKey (KeyCode.L) && Time.time > nextFire) {
 
             //Increment the counter
             nextFire = Time.time + (fireRate * DOUBLE_SHOT_DELAY);
 
             //Instantiat the shot at their repsective positions
             Instantiate (shot, leftShotSpawn.position, leftShotSpawn.rotation);
             Instantiate (shot, rightShotSpawn.position, rightShotSpawn.rotation);
         }
 
         //Left fire button pressed
         if (Input.GetKey (KeyCode.J) && Time.time > nextFire) {
 
             nextFire = Time.time + fireRate;
             Instantiate (shot, leftShotSpawn.position, leftShotSpawn.rotation);
         }
 
         //Right fire button pressed
         if (Input.GetKey (KeyCode.L) && Time.time > nextFire) {
 
             nextFire = Time.time + fireRate;
             Instantiate (shot, rightShotSpawn.position, rightShotSpawn.rotation);
         }
 
 
     }
 
     void FixedUpdate () {
 
         //Reset the velocity
         rb.velocity = Vector2.zero;
 
         //Move left
         if (Input.GetKey (KeyCode.A)) {
 
             rb.velocity = Vector2.left * speed;
         }
 
         //Move right
         if (Input.GetKey (KeyCode.D)) {
 
             rb.velocity = Vector2.right * speed;
         }
 
         //Clamp the ships position on screen
         rb.position = new Vector2 ( Mathf.Clamp (rb.position.x, -X_BOUNDARY, X_BOUNDARY), 0.0f);
 
     }
 
     public void SetSpeed (float newSpeed) {
 
         this.speed = newSpeed;
         
     }
 }
 

Note: I am trying to keep the graphics (temporary graphics that I put together) separate from the code/physics/etc. Is this not a best practice for 2D. I saw it done in the project videos. Could this be my problem?

screenshot-2017-01-10-155134.png (426.3 kB)
Comment
Add comment · Show 11
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 Scoutas · Jan 11, 2017 at 02:34 AM 0
Share

I would think that the problem rises from the fact that you are trying to do these things seperately. $$anonymous$$ind editing in the physics script, where it checks for collisions?

avatar image Kk1496 · Jan 11, 2017 at 02:52 AM 0
Share

The collision is done in the usual OnTriggerEnter();

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Target : $$anonymous$$onoBehaviour {
 
     [SerializeField] private int points;
 
     void OnTriggerEnter2D (Collider2D col) {
 
         if (col.gameObject.tag == "Shot") {
 
             GameController.gameController.AddScore (points);
             Destroy (col.gameObject);
             Destroy (this.gameObject);
         
         }
 
         if (col.gameObject.tag == "Player") {
 
             Time.timeScale = 0;
 //            Destroy (col.gameObject);
 //            Destroy (this.gameObject);
 //            GameController.gameController.SetGameOver (true);
 //
         }
     }
 }

I'm checking two tags in the same function. Not a good idea? I put separate Poly coliders on each half of the ship with the same result.

avatar image Scoutas · Jan 11, 2017 at 03:27 AM 0
Share

I would suggest seeing if it really is triggering early. Set the timeScale to a low number and try to see where it triggers.

avatar image Kk1496 · Jan 11, 2017 at 03:35 AM 0
Share

The screenshot shows what happens when its triggered. The game is supposed to be over but I commented that stuff out to stop the time and see what's happened. Is there anything in particular to look at?

avatar image Scoutas · Jan 11, 2017 at 03:43 AM 0
Share

$$anonymous$$aybe try switching Polygon Collider to a simple Box Collider? To be honest, I'm not seeing the problem, nor in the collision code, nor in the movement ShipController code, so I'm shooting blindly now, but step by step we can try get down to the problem.

Before ending the game, you could log position of the object that triggered the end of the game and see where it is in the scene.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

172 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

LookAt() disables the collision of my 2d object 2 Answers

Destorying objects on collsion 1 Answer

2D RigidBody slightly penetrates objects causing a bounce effect. 3 Answers

Why/How 2d tower of blocks collapse? 0 Answers

Collider fires consistently, but Trigger does not? 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