Move rolling ball roll forward based on the direction it is facing
I am new to Unity and have an issue. I have an accelerometer script on a ball to make it roll around. The ball rotates on the world axis, so when phone is tilted left it moves left and same for forwards, backwards and right. How can I make it so when I turn the ball left I can then tilt my phone forward and it will continue in that direction? I also have a script that follows the direction that the ball is moving in.
Thanks again!
Accelorometer script:
using UnityEngine;
public class Accelerometer : MonoBehaviour { public float moveSpeed = 10;
public bool isFlat = true;
private Rigidbody rigid;
private void Start()
{
rigid = GetComponent<Rigidbody>();
}
private void Update()
{
Vector3 tilt = Input.acceleration;
if (isFlat)
tilt = Quaternion.Euler(90, 0, 0) * tilt * moveSpeed;
rigid.AddForce(tilt);
Debug.DrawRay(transform.position + Vector3.up, tilt, Color.cyan);
}
}
Hi @hunterperatt13 -
Your description is bit hard to understand, so I'm guessing. If your ball forward is facing to movement direction, you can use transform.forward for your movement vector.
See manual: https://docs.unity3d.com/ScriptReference/Transform-forward.html
Thank you for the reply! I am sorry that it was hard to understand. Where would I put transform.forward in my script? Will I have to set the tilt function to only rotate the ball when tilted left or right to perform that function?
Your answer
Follow this Question
Related Questions
Few questions relatedto brick breaker game 0 Answers
Rolling a ball forward with AddForce depending on the track direction 0 Answers
How do I make a ball have force where it bounces and adds force in the direction it is going at? 0 Answers
How to make a ball go in a random direction 0 Answers
Initial accelerometer parameters to move a ball using Vuforia AR 2 Answers