I am encountering a peculiar problem when updating a piece of code that translates my gameobject left and right based on mobile touch input.
To provide context, the platform upon which the gameobject, which is a sphere in my case, translates, is this:
The platform is 10 units wide, with the sphere in the center.
In its original form, the code looks like this:
void FixedUpdate () {
if(Input.touchCount == 1) {
float touchXComponent = Input.GetTouch(0).deltaPosition.x;
transform.Translate(Vector3.right * Time.smoothDeltaTime * touchXComponent * 0.5f);
}
}
It provides the desired left and right movement, but it has a tendency to allow the ball to leave the platform, and then start moving up and down in the air with each further touch.
To combat that, I tried editing the code as follows:
void FixedUpdate(){
if (Input.touchCount == 1){
float touchXComponent = Input.GetTouch(0).deltaPosition.x;
transform.Translate((Vector3.right * Time.smoothDeltaTime * touchXComponent * 0.5f).x, transform.position.y, transform.position.z);
}
}
This, however, causes the ball to fly of the screen the second it is touched, leaving me with this error in the console:
Assertion failed: Invalid worldAABB. Object is too large or too far away from the origin.
I would have thought that the update I made to the code would keep the ball stationary on the y and z-axes, while allowing it to translate freely on the x-axis. Why is that not the case?
Thank you.