I have to make a ball to move due to player’s linear accleration / tilting of phone. this works fine when tested in unity remote, but the ball object doesn’t move when i build and install it in the phone.
pls help to resolve this and as a beginner i expect to get easy to understand answers rather than just solving the issue.
thanks.
Below is the code…
using UnityEngine;
public class BallController : MonoBehaviour
{
public float speed = 10f; // Speed multiplier
public float sensitivity = 2f; // Tilt sensitivity
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// Read accelerometer input
Vector3 tilt = Input.acceleration;
// Adjust sensitivity and map to XZ plane
float moveX = tilt.x * sensitivity;
float moveZ = tilt.y * sensitivity;
// Apply force to move the ball
Vector3 movement = new Vector3(moveX, 0f, moveZ) * speed;
rb.AddForce(movement, ForceMode.Acceleration);
// **Ensure Y velocity stays zero to prevent bouncing**
rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
}
}
Your easy answer is you need to debug the problem before you can fix it.
For instance:
Are you seeing any errors in the adb logcat output?
Are you getting any valid numbers back from the accelerometer at all?
Put in mock numbers… does the rest of the code work?
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
I tried to debug and found out that the accelerometer reading in the android is always (0,0,0). but upon researching there are multiple causes for this problem. i have tried with permission, explicit sensor access and multiple device, but still cant able to identify what and where the actual problem is.
i would really love to hear from somebody on how to resolve this.
thanks for the debugging documentation suggestion, i have learnt to use logcat tool!!.
hope it will get resolved.
Hi Kumarangl004, I am facing the same issue did you found any fixes?
Hi nishanth, i still didn’t find any solutions to it. i have asked few people about this and waiting for help…while still trying to debug it.
@nishanthshamy14293 Hey. Having the same issue. Did any of you find any solution yet?
For anyone still having problems with this, I switched from the new to the old input system and it worked!
just add this code inside the script
void OnEnable()
{
if (Accelerometer.current != null)
{
InputSystem.EnableDevice(InputSystem.GetDevice());
}
}
void OnDisable()
{
if (Accelerometer.current != null)
{
InputSystem.DisableDevice(InputSystem.GetDevice<Accelerometer>());
}
}