How do I restrict the movement of a GameObject along the x-axis?

Updated on December 20, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on December 18, 2019

Hello,

I am working on a 3d game right now and I have been trying out moving a gameobject with a joystick. So far I have been able to get the gameobject to move along the x-axis in both positive and negative directions. But the issue that I am running into now is I want the gameobject to stop moving after it reaches a certain distance along the x-axis in both positive and negative directions. Currently what is happening is the gameobject is allowed to move either left or right forever and there is no restriction. I am wondering if anybody has any suggestions for how I can add the restriction.

Here is the code that I have that moves the player along the x-axis with the joystick

    private void FixedUpdate()
    {
    if (gm.joystick.Horizontal >= .1f)
    {
    transform.Translate(Vector3.right * Time.deltaTime * runSpeed);
    }
    else if (gm.joystick.Horizontal <= -.1f)
    {
    transform.Translate(Vector3.left * Time.deltaTime * runSpeed);
    }
    }

 I am also moving the player forward forever and when the gameobject reaches that point on the x-axsis I dont want the gameobject to stop moving forward. Here is the code for moving the gameobject forward

 

    if (isJumping == false)
    {
    rb.AddForce(Vector3.forward * Time.deltaTime * forceMulti);
    }

 

the jumping bool is there for when the game pauses and nothing else.

 

If anybody has any suggestions for what I could do that would be great!

Thanks!

  • Liked by
Reply
1 on December 19, 2019

You need to keep track of its original position as well as a min and max distance. You can limit the X movement the same way.

Vector3 originalPos;
public float minDist, maxDist;

 

You can set the original position anywhere, but unless you plan on changing it at runtime, the Start function would be ideal. Then there are 2 ways you could go about doing moving and limiting it: you could check if the position is past the max/min before moving it anymore, or you could have a ‘goalPosition’ that you clamp between the min/max, and just always try to move to it.

 

This is how you could do it the first way.

private void FixedUpdate()
 {
     float distance = transform.position.x - originalPos.x;
     if (gm.joystick.Horizontal >= .1f && distance < max)
     {
     transform.Translate(Vector3.right * Time.deltaTime * runSpeed);
     }
     else if (gm.joystick.Horizontal <= -.1f && distance > min)
     {
     transform.Translate(Vector3.left * Time.deltaTime * runSpeed);
     }
}

This way will really only work if the object is moving along the global x-axis though. If you want it to work in any direction and diagonals, you’ll probably want to use the goal method mentions above, but that way does require a bit more set up.

 

Edit: Forgot to mention that you’ll want your minDist to be negative, since its moving backwards to get there. Either that, or 0 and it will only go forward, and back to its original X.

on December 20, 2019

Hey,

Thanks so much for the suggestion it workes exactly how I wanted it to!

 

Thanks

Show more replies
  • Liked by
Reply
Cancel