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:

enter image description here

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.

  • 1
    \$\begingroup\$ Does the sphere have a Rigidbody? In many cases, mixing rigid body physics simulation and direct transform manipulation can cause hiccups and unexpected behaviours. So if you're using physics you generally want to do all your work physically and stay hands-off with the transform component. \$\endgroup\$ – DMGregory Jun 27 '18 at 10:02
  • \$\begingroup\$ @DMGregory Thank you. I'll try using something like AddForce instead. On another note, were the updates to my other question (about getting smooth movement like current popular games) any good? \$\endgroup\$ – user117136 Jun 27 '18 at 10:04
  • \$\begingroup\$ If other users are upvoting or answering your question, that's a sign they find it clear and high-quality. If it's downvoted or idling without activity, that's usually a sign it could benefit from clarifying edits. You don't need me to give a personalized rating — the reception of the site's community is what matters. \$\endgroup\$ – DMGregory Jun 27 '18 at 10:13

As this doc says :

If relativeTo is left out or set to Space.Self the movement is applied relative to the transform's local axes.

Which means you're actually translating your object by it's own position every update. To avoid that just change your call to :

transform.Translate((Vector3.right * Time.smoothDeltaTime * touchXComponent * 0.5f).x, 0, 0);
  • \$\begingroup\$ Thank you. After making the changes, if you drag the screen, the ball still sometimes lifts into the air. \$\endgroup\$ – user117136 Jun 27 '18 at 8:35
  • \$\begingroup\$ For this problem, you should freeze Y position of your object. Either by using rigidbody.constraints, or by setting the Y position to a constant every update. \$\endgroup\$ – Sasugasm Jun 27 '18 at 8:44
  • \$\begingroup\$ I wanted to do that, but there are other game elements where the ball will be falling, in which case I do want its Y value to change. I need to figure a way to translate the object independent of it y-axis transform. Also, I can't upvote your answer because I only have 14 rep. \$\endgroup\$ – user117136 Jun 27 '18 at 8:47
  • \$\begingroup\$ I'd add a collider to your object so that it will collide with the plane, let gravity make it fall when there is a hole and add a constraint to freeze X axis. If the X axis matters, you may may clamp it's position for every update between two constant values. \$\endgroup\$ – Sasugasm Jun 27 '18 at 8:51
  • \$\begingroup\$ Thank you. That makes sense. Can I ask you one last question? Is the method I'm using the best way to get smooth movement from touch input? I was thinking something involving Vector3.Lerp() or some other easing function would be better, but I'm unsure how to go about that. \$\endgroup\$ – user117136 Jun 27 '18 at 8:54

From what I understand, you want the ball to move on the X axis only, relative to world coordinates. The current method you're using will do it in the local coordinates (relative to the object).

Since Transform.Translate method already gives you this functionality, you can just change your line from

transform.Translate((Vector3.right * Time.smoothDeltaTime * touchXComponent * 0.5f).x, transform.position.y, transform.position.z);


transform.Translate((Vector3.right * Time.smoothDeltaTime * touchXComponent * 0.5f).x, transform.position.y, transform.position.z, Space.World);

and it should work in theory (I don't have access to Unity right now).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy