- Home /
Question by
Shady1112 · Apr 24, 2017 at 02:48 PM ·
androidaccelerometer
adding speed make the ball bounce
okay so I'm using this script for my ball to move with the accelerometer but when I add the speed it starts to bounce like crazy to be specific when I add "transform.Translate" line that happens I'm still new to this any help will appreciate it
using UnityEngine;
using System.Collections;
public class Accelerometer : MonoBehaviour
{
public float speed = 5.0F;
public bool isflat = true;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody> ();
}
void Update()
{
Vector3 tilt = Input.acceleration;
if (isflat)
tilt = Quaternion.Euler (90, 0, 0) * tilt;
rb.AddForce(tilt);
transform.Translate(tilt * speed * Time.deltaTime);
}
}
Comment
Answer by krisuman · Apr 25, 2017 at 05:11 AM
You shouldn't use transform.Translate and rb.AddForce at once. If your object is physic to move it you should use only Rigidbody without modifying transform. It is also good practice to do all physic calculations in FixedUpdate.