- Home /
point system for car game
Hi! I want to make a point system for my car game. Here is the code for jumping tricks (pretty simple)
using UnityEngine;
using System.Collections;
public class Jumping : MonoBehaviour { public GameObject nissan350z;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey("1"))
{
transform.Rotate(Vector3.forward * (100*Mathf.PI) * Time.deltaTime);
}
if(Input.GetKey("2"))
{
transform.Rotate(Vector3.up * (100*Mathf.PI) * Time.deltaTime);
}
if(Input.GetKey("3"))
{
transform.Rotate(Vector3.down * (100*Mathf.PI) * Time.deltaTime);
}
if(Input.GetKey("4"))
{
transform.Rotate(Vector3.left * (100*Mathf.PI) * Time.deltaTime);
}
if(Input.GetKey("5"))
{
transform.Rotate(Vector3.right * (100*Mathf.PI) * Time.deltaTime);
}
} }
So I wondering if it's possible to make a system that each time I push 1,2,3,4 or 5, and land properly, I should get a certain amount of points. Thanks, any help will appreciated. PS: I am new in coding/scripting.
Answer by Flash · Apr 20, 2012 at 02:29 AM
using UnityEngine;
using System.Collections;
public class Jumping : MonoBehaviour { public GameObject nissan350z;
private int tmpScore = 0;
// Update is called once per frame
void Update () {
if(Input.GetKey("1"))
{
transform.Rotate(Vector3.forward * (100*Mathf.PI) * Time.deltaTime);
tmpScore += 5;
}
if(Input.GetKey("2"))
{
transform.Rotate(Vector3.up * (100*Mathf.PI) * Time.deltaTime);
tmpScore += 10;
}
if(Input.GetKey("3"))
{
transform.Rotate(Vector3.down * (100*Mathf.PI) * Time.deltaTime);
tmpScore += 15;
}
if(Input.GetKey("4"))
{
transform.Rotate(Vector3.left * (100*Mathf.PI) * Time.deltaTime);
tmpScore += 20;
}
if(Input.GetKey("5"))
{
transform.Rotate(Vector3.right * (100*Mathf.PI) * Time.deltaTime);
tmpScore += 25;
}
}
}
This code will add 5,10,15,20,25 relative to the number keys pressed (1,2,3,4,5) to a tmpScore variable. Now what you need is a way to determine if the car has landed properly... This could be done with colliders/triggers under the car or through a more complicated script. Once the car has landed properly you will need to add the tmpScore to the current score of your player.
score += tmpScore
I will need more information about the game (Probably need to see a live demo) in order to figure out which solution would be best for checking if the car lands properly.
Thank you veyr much for your reply!
I have 4 wheelcolliders, so I was thinking, if a player lands properly with atleast 1 wheel on the terrain, he gets points for that. And the points should be counted in the small gui box.
I hope you will be able to help me with that. Thanks in advance!
You can try something like this.
function OnCollisionEnter(collision : Collision) { if(collision.gameObject.name == "terrain"){ //Add score to player. } }
Here's more help on OnCollisionEnter: http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html Here's some info about the Collision: http://unity3d.com/support/documentation/ScriptReference/Collision.html
As for displaying the score on screen simply use:
function OnGUI(){ GUI.Label(Rect(10, 10, 100, 25), "Score: "+Score); }
Your answer
Follow this Question
Related Questions
Point system for car game 0 Answers
Accessing local system ( File Browser ) 2 Answers
Car Script error C# 2 Answers
game system requirements 1 Answer
Simple Point system 3 Answers