Car crash scoring

Hi,

I have a player controlled car driving on a track, and I’d like to give the player points when they collide with another object (a moving car, for example). Is there an easy way to do this by reading velocity, angle of impact etc from the physics system, then assigning a score?

Thanks.

You can do that yes. An easy way? Define easy.

This is the information the physics system gives you when a collision happens.

Thanks, could someone show me a sample code to assign a points score to relativeVelocity please? I’m not that good with java :slight_smile:

Not enough info on your code to give you code you can copy verbatim, but here’s the basic logic (it’s pretty simple).

public float currentScore;

void OnCollisionEnter(Collision collision)
{
    AddScore(collision.relativeVelocity.magnitude)
}

public void AddScore(float score)
{
    currentScore += score;
}

You could always add a modifier:

public float currentScore;
public float modifier;

void OnCollisionEnter(Collision collision)
{
    AddScore(collision.relativeVelocity.magnitude * modifier)
}

public void AddScore(float score)
{
    currentScore += score;
}

Edit: You mentioned java (I’m assuming you mean javascript / unityscript). To convert this to JS just replace “void” with “function” and replace the variable decleration with “public currentScore : float”.

You need to decide how you want points awarded first. Is it a fixed number of points just for hitting another vehicle? Do you want the points to reflect how hard you hit the other vehicle? Do you want point to reflect how much you change the other vehicle’s course? etc.

Also, assigning a points score to relativeVelocity doesn’t make a lot of sense, unless you have some very strange game mechanics. You will need to elaborate more about what you really want. Finally, as was mentioned above, simply polling the Collision struct from the OnCollisionEnter() method should provide you will all the data you need to create whatever point system you want.

Fwiw, there’s no need to say “I’m not good with Java.” If you are here, trying to learn, then it doesn’t matter how good or bad you are. We’re glad to have you (welcome!) and help you. Usually when people write “I’m not good with Javascript/programming/coding” all it means is “I can’t be bothered putting any effort into learning to do it myself, so can someone do it for me”. And I don’t think that’s what you mean :slight_smile: