Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Regarding Accelerometers' Tilt Sensitivity

Discussion in 'Scripting' started by ZigmasNewbie, Aug 6, 2022.

  1. ZigmasNewbie

    ZigmasNewbie

    Joined:
    Aug 6, 2022
    Posts:
    2
    Hi everyone, I've been playing around with Unity for a little bit and decided to make a basic "roll a ball" game using accelerometer on phones, but I want to also make it on desktops as well.. The problem is with handheld (phones) devices that I have to tilt the phone quite a bit to move it to the direction I want the ball to go to. I was just wondering how would I adjust the tilt sensitivity on Phones, but still keep the speed the clamped to the same as if I played on PC? I hope I make sense, I really want to learn.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float tiltSensitivity = 10;                                                                                                     // Tilt sensivity of Handheld Devices
    7.     public float movementSpeed = 100;                                                                                                      // Movement speed of the character
    8.  
    9.     void FixedUpdate()
    10.     {
    11.         if (SystemInfo.deviceType == DeviceType.Desktop)                                                                                    // If the game is running on PC
    12.         {
    13.             Debug.Log("Playing on PC");                                                                                                     // Print out that we are playing on PC
    14.             float moveHorizontal = Input.GetAxis("Horizontal");                                                                             // Get Horizontal Input on PC
    15.             float moveVertical = Input.GetAxis("Vertical");                                                                                 // Get Vertical   Input on PC
    16.             Vector3 movementDirection = new Vector3(moveHorizontal, 0.0f, moveVertical);                                                    // Calculate Movement Direction Based on Input
    17.             GetComponent<Rigidbody>().AddForce(movementDirection * movementSpeed * Time.deltaTime);                                         // Apply Movement Direction to RigidBody
    18.         }
    19.         else if (SystemInfo.deviceType == DeviceType.Handheld)                                                                              // If the game is running on Handheld device
    20.         {
    21.             Debug.Log("Playing on Mobile");                                                                                                 // Print out that we are playing on Handheld Device
    22.             Vector3 movementDirection = new Vector3(Input.acceleration.x * tiltSensitivity, 0.0f, Input.acceleration.y * tiltSensitivity);  // Calculate Movement Direction Based on Accelerometer
    23.             GetComponent<Rigidbody>().AddForce(movementDirection * movementSpeed * Time.deltaTime );                                        // Apply Movement Direction to RigidBody
    24.         }
    25.     }
    26. }
    I use tiltSensitivity to multiply Input.acceleration but it just speeds up the movement of the ball on Mobile, but when I play on PC it just doesn't move as fast. In future, I'd love to have an option about changing Sensitivity of tilt on phones just for Accelerometer, that's why I am just curious how would I make the speed of player same on both PC and Handheld devices.
     
    Last edited: Aug 6, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Generally:

    1. gather the input (depending on what target)

    2. normalize the input (to the extent possible, depending on target)

    3. act on the input (apply all your game logic speed scales / offsets here)

    Keep in mind also that every phone will have subtly different accelerometer responses.

    For #1 and #2 your program can decide based on the value of
    Application.platform
    , or else use conditional compiles (I prefer the former so that all code is always compiled... keeps code from going stale).
     
    Last edited: Aug 6, 2022
  3. ZigmasNewbie

    ZigmasNewbie

    Joined:
    Aug 6, 2022
    Posts:
    2
    While I truly do appreciate your input on this, I cannot apply it to any extent since I have 0 understanding on this. I don't know how to normalize the input or act on normalized input..

    I appreciate your response, I guess I'm just too new to programming to understand it.. Could you please translate it to code and explain it for me if it's not too difficult?