1

I want to develop racing game in 2D using Accelerometer with Unity. The sensor will make car rotate left or right like this figure. enter image description here

When i flip smartphone to right, car will turn right, and when i flip to left, car will turn left. I use accelerometer sensor to turn the car when flip smartphone.

My Code :

    public class accel : MonoBehaviour 
    {
        public MoveController mc;
        void Update () 
            {
                transform.Translate(0,-Input.acceleration.x, 0);
                if (Input.acceleration.x > 0) {
                    mc.degree -= 10;
                    mc.derajat-=10;
                }
                else if (Input.acceleration.x < -0) {
                    mc.degree += 10;
                    mc.derajat+=10;
                }
                else {
                    mc.degree += 0;
                    mc.derajat+= 0;
                }
            }
}

Class MoveController :

using UnityEngine;
using System.Collections;

public class MoveController : MonoBehaviour {

    public bool stop = false;
    public enum ButtonList {left,right,forward,stop};
    public float currspeed = 1.0f;
    public float acceleration = 5f;
    public float maxspeed = 12f;
    public float degree = 10f;
    public float derajat = 10f;
    public bool jalan;
    public Camera camera;
    public SpriteRenderer redcar;
    public float sudut;

    public void setActiveStop() {
        stop = true;
    }

    public void setUnactiveStop() {
        stop = false;
    }

    public bool getStop() {
        return stop;
    }

    public bool getNotStop() {
        return !stop;
    }

    void Start () {
        this.setUnactiveStop ();    
        jalan = false;

    }

    // Update is called once per frame
    void Update () {
        this.start_game (); 

    }

    public void start_game() {
        if (getNotStop()) {
            redcar.transform.rotation=Quaternion.Euler(0,0,degree);
            camera.transform.rotation=Quaternion.Euler(0,0,derajat);

            sudut=redcar.transform.rotation.z;
            redcar.transform.Translate((currspeed*Mathf.Cos(sudut*Mathf.PI/180))*1*Time.deltaTime,
                                       (currspeed*Mathf.Sin(sudut*Mathf.PI/180))*1*Time.deltaTime,
                                       0);
            camera.transform.Translate((currspeed*Mathf.Cos(sudut*Mathf.PI/180))*1*Time.deltaTime,
                                       (currspeed*Mathf.Sin(sudut*Mathf.PI/180))*1*Time.deltaTime,
                                       0);

        }
        jalan = true;

    }
}

When i build and run in my smartphone, the car doesn't stop to rotate and its rotation is too fast. I want to modify Update function in first code, in order to the car can rotate / turn right-left smoothly.

So, how to fix it?

Nain
  • 1,204
  • 1
  • 12
  • 17
ariyandi
  • 149
  • 2
  • 15

1 Answers1

2

Currently your problem is that unless the accelerometer is exactly 0 you will be turning 10 degrees every frame to the right or left. Also adding or subtracting 10 degrees every frame is the reason you are turning too quickly.

One approach would be to have a threshold where you are not turning. This can be accomplished by changing the value you compare Input.acceleration.x against. I also changed angle that you rotate by every frame to use Time.deltaTime.

The time in seconds it took to complete the last frame (Read Only). Use this function to make your game frame rate independent.

This means that all cars should turn at the same speed on all devices.

Below I made some updates to your update function. You will need to play around with the SPEED and THRESHOLD variables to get the turning to feel right.

SPEED: how quickly the car turns

THRESHOLD: how far the device needs to be tilted before the turning begins

public class accel : MonoBehaviour 
{
    public MoveController mc;
    private float SPEED = 0.5;
    private float THRESHOLD = 0.3;
    void Update () 
    {
        transform.Translate(0,-Input.acceleration.x, 0);
        if (Input.acceleration.x > THRESHOLD) {
            mc.degree -= Time.deltaTime * SPEED;
            mc.derajat-= Time.deltaTime * SPEED;
        }
        else if (Input.acceleration.x < -THRESHOLD) {
            mc.degree += Time.deltaTime * SPEED;
            mc.derajat+= Time.deltaTime * SPEED;
        }
    }
}
Mike G
  • 668
  • 1
  • 6
  • 17