How to control OnTriggerEnter() and Reflect() depending on the direction of travel of an object.

Hey, so I have a scene where a ball will bounce off of a line drawn by a user. As it is, when the ball hits the line, I just call the reflect function in unity. What I’ve noticed, is that occasionally the ball will stick to the line and then continue along its trajectory.

I didn’t notice the cause at first due to the objects in the scene, but as it turns out if the user draws a line behind the ball, but close enough where the colliders touch, OnTriggerEnter is run. Obviously this screws things because the ball is reflected back into the line.

I have written a quick couple of if statements shown below as a proof of concept of my fix. Basically this is for a quadrant where x is positive y is positive. If the velocity of the ball is in the positive x direction and the x component of the contact is greater than the position of the ball(the ball is hitting a line in front of it and not behind), the reflect function runs.

My question: Is there an easier way to do this? I mean this is pretty straight forward and here isn’t too much to the code, but as I have it drawn out now, I will need four sets of if statements for each quadrant, which will give me 16 copies of the code below.

I would think this comes up quite a bit and maybe there’s a faster and more elegant solution? I’m asking for my own knowledge for one. Two, if I want to change something later, this could turn into a jumbled mess pretty easily. Three, is this going to be taxing when running on mobile?

    function OnCollisionEnter(enterInfo : Collision){  //When we run into something
    
    var colliderContact : ContactPoint;
    colliderContact = enterInfo.contacts[0];
    
    	if(enterInfo.collider.gameObject.layer == LayerMask.NameToLayer("DrawnLine")){
    
    		if(colliderContact.point.x >= 0 && colliderContact.point.y >=0){
    Debug.Log("1");
    			if(velocity.y >= 0 && velocity.x >=0){
    Debug.Log("2");			
    				if(transform.position.x <= colliderContact.point.x){
    Debug.Log("3");
    					this.gameObject.layer = 10;
    					
    						for(var contact : ContactPoint in enterInfo.contacts){ //Find collision point
    				
    					        velocity = Vector3.Reflect(velocity, contact.normal);
    					        velocity *= 1.1;
    	   				}
    	    		}
    	    	}
    	    }
    	}

Well, I have a solution that works pretty good. As usual, I made the problem more difficult than it had to be. Basically looking at the sphere, if it is moving in the positive x direction the collision needs to happen on the right hemisphere in order for there to be a bounce. This works for any arbitrary position, negative or positive. The same goes for all other directions, if the sphere is traveling in the negative y direction, the collision needs to occur on the bottom hemisphere. I’ve attached a condensed version of the code below.

function OnCollisionEnter(enterInfo : Collision){  //When we run into something

var colliderContact : ContactPoint;
colliderContact = enterInfo.contacts[0];

	if(enterInfo.collider.gameObject.layer == LayerMask.NameToLayer("DrawnLine")){
		
		if((velocity.y >= 0 && transform.position.y <= colliderContact.point.y) || (velocity.y <= 0 && transform.position.y >= colliderContact.point.y)
		 ||(velocity.x >= 0 && transform.position.x <= colliderContact.point.x) || (velocity.x <= 0 && transform.position.x >= colliderContact.point.x)){		
				
			this.gameObject.layer = 10;
			
				for(var contact : ContactPoint in enterInfo.contacts){ //Find collision point
		
				velocity = Vector3.Reflect(velocity, contact.normal);
				velocity *= 1.1;
	 		}
	 	}