Unity3D movement with left and right button changes on rotation

Started by
5 comments, last by Zakwayda 4 years, 7 months ago

Hello guys I am a bit confused on rotation and position in unity. The thing is when I am at 0 rotation on X-Axis and I restricted my player to not let it go more than 10 on x-axis and less than -10 x-axis but when I rotated it to 90 degree It stated to move forward on left key press and backward to right key press. I just want to clamp position between 10 and -10 on every rotation It must not move differently when rotated... e.g for some understanding the scenario If a player is on a road and it is restricted to go on foot path then it should also be restricted when rotated to left or right... Please help me with this guys any help would be appreciated. 
Thank You.

Advertisement

This sounds like more of a programming question than an art question. In any case, what you describe sounds like the object is being moved along a local axis rather than along the global x axis, but we'd probably need to see some code to be sure. So, maybe you could post the bit of code that moves the object.

Thanks mate this is the code.

 

        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(0, 0.0f, moveVertical * speed);

        if (playerPalm[0].bounds.Intersects(signBoard[0].bounds) && rotate == false)
        {
            rotateTo -= 90;

            rotate = true
        }
        else if (rotate && transform.rotation.z > rotateTo)
        {
            transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.Euler(0, 0, rotateTo), (20 * Time.deltaTime)); //rotate left until desired rotation axis 
        }

        else

           rotate = false;


        if (Input.GetKey(KeyCode.LeftArrow))
            if (!playerPalm[0].bounds.Intersects(pathBound[0].bounds)) //To keep player stay away from left foot path for now using colliders to avoid need to remove this collider thing and want to do it programatically
                transform.position -= new Vector3(speed, 0, 0) * Time.deltaTime;

        if (Input.GetKey(KeyCode.RightArrow))
            if (!playerPalm[1].bounds.Intersects(pathBound[0].bounds)) //To keep player stay away from right foot path
                transform.position -= new Vector3(speed, 0, 0) * Time.deltaTime;

        transform.Translate(0, moveVertical * speed * Time.deltaTime, 0);

There are some aspects of the code I'm not quite clear on, but I'll make one observation. Here:


transform.Translate(0, moveVertical * speed * Time.deltaTime, 0);

You're using an overload of Transform.Translate() that assumes local space (as I suspected). Try using an overload that allows you to specify the space, and specify world space. (Or you could just modify the position directly, which you're doing elsewhere anyway.)

Here is the scenario there nothing to do with left right movement with transform.translate but instead it's happening in if check as I mentioned above

Untitled.png

2 hours ago, syedMohib44 said:

...as I mentioned above

Your original post was a bit hard to parse for me, so maybe I didn't understand what you meant. My only thought at the moment is that maybe it's the translations in the 'if' statements that need to be in a different space - local instead of global in this case. In other words, you could try using Transform.Translate() there (using the 'local space' option, implicitly or explicitly) instead of modifying the position directly. Again though, there are some things in your code that don't make immediate sense to me, so I'm guessing a bit here.

[Edit: Upon reflection, it does seem like maybe you intended for those corrective translations to be local rather than global. That would seem to make a certain amount of sense given what you've written.]

This topic is closed to new replies.

Advertisement