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. 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?
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?
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.
I would suggest seeing if it really is triggering early. Set the timeScale to a low number and try to see where it triggers.
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?
$$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.