Entry 6: The Game Mechanism and How It's Done

So what is all the mechanism I need for _TRANSPORT's mechanism
- an invisible force that force the car to go forward in high speed
- "You Died" system << the fix for the PHASING THROUGH SIDE WALLS bug
- a backup steering control w/ keyboard

Since the game require the mechanic that somewhat similar to temple run, the tutorial used:


YouTube. (2018). Unity Endless Tutorial • 0 • Introduction [Tutorial][C#]. [online] Available at: https://www.youtube.com/watch?v=7jdL5538bEo&index=1&list=PLLH3mUGkfFCXps_IYvtPcE9vcvqmGM pRK [Accessed 30 Jan. 2018].

N3K EN shared his series about making a simple endless runner game. His tutorial is really relevant due to the fact this digital game I'm developing is somewhat similar to it.


Technologies, U. (2018). Unity - Scripting API: Rigidbody.AddForce. [online] Docs.unity3d.com. Available at: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html [Accessed 30 Jan. 2018]. 

An official Unity documentation site where it’s about AddForce. My car need to AddForce on forward to go forward.


Unity. (2018). Limit a rigidbody axis to a range. [online] Available at: https://answers.unity.com/questions/495564/limit-a-rigidbody-axis-to-a-range.html [Accessed 30 Jan. 2018].

 A forum about limiting axis to a range. I’d use this in the coding where limiting the speed of the car so that it won’t go faster than a specific speed.


Unity. (2018). How do add force to the left (x-axis). [online] Available at: https://answers.unity.com/questions/661578/how-do-add-force-to-the-left-x-axis.html [Accessed 30 Jan. 2018]. 

A forum about how to add force on the x-axis. I use this source to help in my coding to program my vehicle to go left and right.


Unity. (2018). Limit a rigidbody axis to a range. [online] Available at: https://answers.unity.com/questions/495564/limit-a-rigidbody-axis-to-a-range.html [Accessed 30 Jan. 2018]. 

A forum about limiting axis to a range. I’d use this in the coding where limiting the speed of the car so that it won’t go faster than a specific speed.



During this technical progress, the asset I'll be using is Unity Assets temporally.

1. Invisible forward force

I've created a C# script that DEDICATE on going forward and clamp the speed limit which is fairly simple.
*Dedicate* = since I'm going to need to disable it for debugging.


These are the code:







using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class forwardForce : MonoBehaviour
{
    public float thrust;
    public Rigidbody rb;

    public float maxSpeed;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody>();
     
    }

    // Update is called once per frame
    void Update()
    {
        rb.AddForce(transform.forward * thrust); //   //"Addo forco"


        rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);  //This limit the speed
    }
}






2. "Die" mechanism
My intended mechanism that the player will lose when the car collide on the front. Due to the PHASING bug, I need to improvise.
How I'm going to do this is whenever my car hits a boxCollider, the scene reloads so it'll start from the beginning of the current scene.

How I did this is:

Step 1: I've made a box collider and place it into the car <<should have use the existing box collider instead.

Step 2: Create a C# script to trigger dead function when the car collide with another collider using the OnTriggerEnter() function.







using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class triggerDead : MonoBehaviour {

    public string toScene;

    // Use this for initialization
    void Start () {

    }
// Update is called once per frame
void Update () {

}

    private void OnTriggerEnter(Collider other)
    {
        dead();
    }

    void dead()
    {

        SceneManager.LoadScene(toScene);
    }
     
}

3. Backup steering
I'll need it to addForce which similar to "Invisible Forward Force" but along the x-axis instead of z-axis. It's pretty straight forward.


Here's the code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class steering : MonoBehaviour
{

    public float steeringPower;
    public Rigidbody rb;

    Vector3 position;
    public float maxSteeringSpeed;

    // Use this for initialization
    void Start()
    {
     
        rb = GetComponent<Rigidbody>();
        position = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity = Vector3.ClampMagnitude(rb.velocity= Vector3.right, maxSteeringSpeed);

            if (Input.GetKey("right"))
        {
            rb.AddForce(Vector2.right * steeringPower);
        }

        if (Input.GetKey("left"))
        {
            rb.AddForce(Vector2.left * steeringPower);
        }

    }
}



That's all for controlling the car using keyboards. If you're finding for Leap Motion controls, check the previous post.

Comments

Popular posts from this blog

Entry 9: Post-Processing in Unity