2
\$\begingroup\$

I have a player ship (cube) and some enemy laser projectiles. When the player ship has position X values greater than 1, collision with the enemy projectiles occur but when it has lower X values, the projectiles pass through.

If the image below is too small to view, you can refer to this link.

enter image description here

There are two screens in the image. The above image is where the collision occurs and the below one is where the laser projectiles miss the player ship. (You can see in a larger image better the tiles. When the collider of the player ship goes below the central horizontal line, collisions do not occur). Let me post the scripts for projectiles and player ship.

LaserProjectile

public class LazerProjScript : MonoBehaviour {

    private float speed = 5.5f;


    // Update is called once per frame
    void Update () 
    {
        // translate
        transform.Translate (new Vector3 (0f, -1f, 0f) * speed * Time.deltaTime);

        // destroy the projectile once it's out of screen
        if(transform.position.y <= -10)
            Destroy(gameObject);
    }

    void OnCollisionEnter(Collision other)
    {

        if(other.gameObject.tag == "Player")
        {
            print ("lazer on collision enter!");
            print ("hit player");
            other.gameObject.GetComponent<PlayerController>().hp -= 1;
            Destroy(gameObject);
        }
        if(other.gameObject.tag == "Enemy") return;

    }

}

Player Controller

public class PlayerController : MonoBehaviour 
{
    public int hp = 100;
    public float speed = 0.3f;

    // Update is called once per frame
    void Update () 
    {
        /*********************************************************/
        /********************** TRANSFORM CODE *******************/
        /*********************************************************/
        //horizontal <- || -horizontal ->
        //vertical down || -vertical up
        float h = -Input.GetAxis ("Horizontal")*speed;
        float v = -Input.GetAxis ("Vertical")*speed;

        // do not let the ship get out of the camera
        if (Input.GetAxis ("Horizontal") < 0 && transform.position.x <= -11.8) {
            h = 0;
        }
        if (Input.GetAxis ("Horizontal") > 0 && transform.position.x >= 11.8) {
            h = 0;
        }

        if (Input.GetAxis ("Vertical") > 0 && transform.position.y >= 8.2) {
            v = 0;
        }
        if (Input.GetAxis ("Vertical") < 0 && transform.position.y <= -8.2) {
            v = 0;
        }

        transform.Translate (h, v, 0);
    }
}

Please note that neither the player ship nor the laser projectiles are isTrigger, isKinematic. Both have their Z values 0 all the time when they translate.

I cannot identify the problem. If the collider is above X=1 axis, there's no problem. If it's below that, collision does not occur! What is this, how can I solve this problem?

EDIT

Well, I played with the positions a bit and it got stranger. If projectiles start moving towards the player when they are in some range, they collide but if they travel for a certain amount of time (i.e. if they are more distant than some distance) they do not collide. I changed the Y values of the projectiles and one collided with the player even though it was below the X=1, and the other just passed through.

EDIT2

If I do not move the player, the projectiles miss. When I move the player, collisions occur whatever the distance was.

\$\endgroup\$

closed as off-topic by Anko, Josh Feb 14 '14 at 17:23

  • This question does not appear to be about game development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Where is the method OnCollisionEnter called? Sorry I use answer for this but, I don't have enough rep to comment :-S \$\endgroup\$ – JarFlux Feb 7 '14 at 20:08
  • \$\begingroup\$ It's in the laserProjectile script, which is attached to the red laser projectiles. I mean, you can see the methods in the codes I posted so I believe you missed it. Otherwise, I do not get your question. \$\endgroup\$ – Varaquilex Feb 7 '14 at 20:12
  • \$\begingroup\$ What is calling your "OnCollisionEnter" method? \$\endgroup\$ – Alexandre Vaillancourt Feb 7 '14 at 20:14
  • \$\begingroup\$ Player doesn't have that method but the projectile has it. So laser projectile calls it ? \$\endgroup\$ – Varaquilex Feb 7 '14 at 20:17
  • 1
    \$\begingroup\$ This question appears to be off-topic because it is about debugging code. Is there a way we could edit it to explain more about the problem and less about the technicalities of a solution? \$\endgroup\$ – Anko Feb 14 '14 at 13:30
2
\$\begingroup\$

I see two things suspect with your code:

  1. You should not be calling transform.Translate() to move a projectile - it's possible that you'll translate it right over its target!

    Instead, attach a rigidbody component, turn off gravity, and set the velocity. If the velocity is high enough, you may also need to set the collision mode to 'continuous'.

  2. This code

    if(transform.position.y <= -10)
        Destroy(gameObject);
    

    is suspicious, since it relies on the absolute position to determine when an object is off-screen.

    Instead, please see this post for the proper way to determine if an object is off-screen.

\$\endgroup\$
  • \$\begingroup\$ I believe your answer misses the link for the 'this post'. Thanks for the answer, rigid body seems to solve it. \$\endgroup\$ – Varaquilex Feb 7 '14 at 20:49
  • \$\begingroup\$ @Varaquilex Oops! Try now. \$\endgroup\$ – BlueRaja - Danny Pflughoeft Feb 7 '14 at 21:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.