I have a problem to solve, how do I get when a moving enemy collides with the player not to accumulate points, unless the player only receive points when collides with the enemy and not when the enemy collides with the player?
To be more precise:
How do I make the player receives points when collides with the enemy and not when the enemy collides with the player?
In my code, receives points when collides with the enemy, and receives points when the enemy collide with the player.
// Add this script to player
var ScoreGUI : GUIStyle;
var scorePos = Vector2(5,95);
static var score = 0;
var scoreOnHit = 5;
function Start()
{
score = PlayerPrefs.GetInt("Pontos"); //get this value.
}
// get point on collisions.
function OnCollisionEnter(col : Collision)
{
if(col.gameObject.tag == "COM")
{
//********Basic points*************************************************************************************
if (col.relativeVelocity.magnitude > 20) // smal collision 5 points.
{
this.score += scoreOnHit;
}
}
}
function OnGUI()
{
//GUI.color = Color.yellow;
//GUI.Label(Rect(scorePos.x, scorePos.y,100,100), score.ToString());
GUI.Box (new Rect(scorePos.x, scorePos.y, 150, 30),"Pontos: " + score, ScoreGUI);
}
@script ExecuteInEditMode()
Does the player have to press a button or have a power-up (think pac-man) in order to touch enemies?
If yes:
when the collision happens:
if the button or power up is pressed/active
add points to the player
else
reduce points from the player
this also means that when the player is still he loses score if hit by enemies.
Basically you need to find a way to understand what the player’s intent is.
Anyway that’s how I handle when my character attacks something he can also receive damage from, if he is attacking he reduces points from the enemy, if he is not attacking and not blocking he receives damage.
The problem that you have is that Unity doesnt see “Player collides with Enemy” and “Enemy collides with Player”, it only see “Object1 collides Object2” It is then up to you to decide how to determine what that collision is, and how it is figured.
Koyima’s powerup method is one way. you can also get the relativevelocity of the impact. (from the Collision) then use Vector3.Dot() against the player’s velocity. Logic then says, if the relative velocity is going the same direction as the player or similar, then the “intent” is that the player has rammed the enemy. If the reverse is true, the enemy has rammed the player, and if the number is around zero, then the impact was not great enough to award points.
Yes, I think this would cover most (if not all) cases. I guess if we knew more details about how the game is setup we could offer more specific assistance.