- Home /
Accelerator/Gyroscope - calculate angle of the tilt on a phone and limiting it?
So I'm trying to make a mobile game where you can tilt your phone to make a cube go left/right. If I tilt my phone the cube tumbles and move either left or right which is how I want it. However if the phone's tilted too much the cube gets an incredible speed which I really do not want.
I have been looking for a solution everywhere and tried a lot of things to fix my issue but it never turns out well. I tried setting a max velocity on the cube but that breaks other mechanics of my game. I've also tried detecting if the phone is in portrait mode, if so the cube got a force the opposite way to cancel the extreme speed but that didn't really solve it either and just turned out wonky.
What I want to achive is: If the phone gets tilted with an angle of 25° or more the velocity won't increase anymore.
Like so:
(Cube wont move) (Cube moves at max speed in the right direction.)
Is it even possible to achieve want I want? If yes, how? If no, what should I do instead?
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
float speed = 25F;
public Rigidbody Cube;
public float currentHeight;
public float previousHeight;
public float travel;
void Start()
{
Cube = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
{
Vector3 accel = Input.acceleration;
Cube.AddForce(accel.normalized.x * speed, 0, 0);
}
//Below is irrelevant for my issue with tilt limit!
//Increasing gravity when the cube is falling.
currentHeight = transform.position.y;
travel = currentHeight - previousHeight;
if (currentHeight < previousHeight)
{
Physics.gravity = new Vector3(0, -25.0f, 0);
}
previousHeight = currentHeight;
}
void OnCollisionStay(Collision col)
{
//Resetting gravity after the cube hits ground.
if (col.gameObject.tag == "Cube")
{
Physics.gravity = new Vector3(0, -9.81F, 0);
}
}
}
Any help would be very much appreciated! :)
If you have the angle of the tilt, you can create a collider that rotates with the phone, but to the maximum of 25°. This way your maximum slope will always be whatever you want. The downside of this is that your object will not be sliding along the edge of the phone.
Just a quick idea, maybe it will help in some way. It's a fun problem tho, something to play around with, I will stalk this thread for some better ideas from other people
Yeah I see what you mean but I haven't come to the stage where I try to solve how to stop the cube from going any faster. I'm stuck at how I'm actually supposed to calculate the tilt of the phone. Hopefully there's someone on here that knows about a good solution that stumbles across this thread because otherwise I don't know how I can figure this out. Thanks for your reply however!
Answer by trusebruse · Sep 22, 2018 at 11:03 PM
I solved this by myself and found out that it was a lot easier than I first thought.
I don't really need to calculate anything to accomplish this. I all needed to do was to take the sqrMagnitude of tilt and setting a value of it which it must exceed before getting any force added to it and vice versa to cease any more force being added at a certain point.
If force is added when the tilt.sqrMagnitude is set to be > 0, the cube would always tilt either left or right but when the tilt.sqrMagnitude is set to 0.05 I can angle the phone from 0 to 0.05 (of some unit which is not degrees), without any force added. When the phone it tilted more than 0.05 the cube starts to accelerate and when the phone is angled more than 0.20, no more force is being added. 0.20 seems to equal to 22.5 degrees.
So that's all, this is the complete code for moving left and right with a cube on a mobile without having the cube accelerate in an insane speed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
float speed = 15f;
public Rigidbody Cube;
void Start()
{
Cube = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
Vector3 tilt = Vector3.zero;
tilt.x = Input.acceleration.x;
if (tilt.sqrMagnitude > 0.05)
{
Cube.AddForce(tilt.normalized.x * speed, 0, 0);
}
if (tilt.sqrMagnitude > 0.20)
{
Cube.AddForce(0 , 0, 0);
}
}