Home > database >  How do I freeze Y-axis Transform, in unity 3D. I want to select any GameObject in the game play Usin
How do I freeze Y-axis Transform, in unity 3D. I want to select any GameObject in the game play Usin

Time:10-13

That the User Game View Panel

Here is an Editor view

That the GameObject Movement in all axis, I don't wants that behavior to move in 3-Directions, I just want it to move in either X-axis OR Z_axis

I'm making a 3d game, in which a user can move vehicles using touch-controls that will allows the user to clear the parking area. this script is allied on the camera. this game this is like a 3D_Traffic_Jam_Parking clearance... I'm confused. I just tried to go on multiple learning platforms, but don't get it...!!!

        public class ZarSwipe2D_Control : MonoBehaviour
        {
            #region Variables
            private float distance;
            private bool isDraging = false;
            private bool swipeLeft, swipeRight, swipeUp, swipeDown;
            public Vector3 desiredPosition;
            private Vector2 Swipe2D;
            private Transform Player;
        
            #region Getter-Setter
            public bool SwipeLeft { get { return swipeLeft; } }
            public bool SwipeRight { get { return swipeRight; } }
            public bool SwipeUp { get { return swipeUp; } }
            public bool SwipeDown { get { return swipeDown; } }
            #endregion
            #endregion
        
            #region Controller Functionality
            private void Update()
            {
                Vector3 v3;
                float x = Swipe2D.x;
                float y = Swipe2D.y;
                Touch touch = Input.touches[0];
                Vector3 pos = touch.position;
                if (Input.touchCount != 1)
                {
                    isDraging = false;
                    return;
                }
                if (touch.phase == TouchPhase.Began)
                {
                    Ray ray = Camera.main.ScreenPointToRay(pos);
                    if (Physics.Raycast(ray, out RaycastHit hit))
                    {
                        if (hit.collider.tag == "Player")
                        {
                            Player = hit.transform;
                            distance = hit.transform.position.z - pos.z;
                            v3 = new Vector3(pos.x, pos.y, distance);
                            v3 = Camera.main.ScreenToWorldPoint(v3);
                            desiredPosition = Player.position - v3;
                            isDraging = true;
                        }
                    }
                }
                if (isDraging && touch.phase == TouchPhase.Moved)
                {
                    v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
                    v3 = Camera.main.ScreenToWorldPoint(v3);
                    Player.position = v3   desiredPosition; 
                }
                if (isDraging && (touch.phase == TouchPhase.Ended || touch.phase ==TouchPhase.Canceled))
                {
                    isDraging = false;
                }
            }
            #endregion
        }

CodePudding user response:

If you are using a rigidbody component there is an option to disable rotation or movement along 1 axis only

CodePudding user response:

#region Variables
    //private float dist;       // for old Code
    private bool dragging = false;
    //private Vector3 offset;   // for old Code
    private Transform toDrag;
    private Vector2 StartSwipePos, EndSwipePos;
    #endregion

    #region Swipe Controller - Axis Along X&Z
    void Update()
    {
        //Vector3 v3;
        if (Input.touchCount != 1)
        {
            dragging = false;
            return;
        }
        Touch touch = Input.touches[0];
        Vector3 pos = touch.position;
        if (Input.touchCount == 1)
        {
            if (touch.phase == TouchPhase.Began)
            {
                StartSwipePos = touch.position;
                dragging = true;

                #region OldCode - Touch Begin
                //Ray ray = Camera.main.ScreenPointToRay(pos);
                //if (Physics.Raycast(ray, out RaycastHit hit))
                //{
                //    if (hit.collider.CompareTag("Player"))
                //    {
                //        toDrag = hit.transform;
                //        dist = hit.transform.position.z - Camera.main.transform.position.z;
                //        v3 = new Vector3(pos.x, pos.y, dist);
                //        v3 = Camera.main.ScreenToWorldPoint(v3);
                //        offset = toDrag.position - v3;
                //        dragging = true;
                //    }
                //}
                #endregion
            }
            if (dragging && touch.phase == TouchPhase.Moved)
            {
                EndSwipePos = touch.position;
                Ray ray = Camera.main.ScreenPointToRay(pos);
                if (Physics.Raycast(ray, out RaycastHit hit))
                {
                    Vector2 Distance = EndSwipePos - StartSwipePos;
                    float x_Distance = Mathf.Abs(Distance.x);
                    float Y_Distance = Mathf.Abs(Distance.y);
                    if (x_Distance > Y_Distance)
                    {
                        if (Distance.x > 0)
                        {
                            if (hit.collider.CompareTag("Player"))
                            {
                                toDrag = hit.transform;
                                toDrag.transform.Translate(25f * Time.deltaTime, 0, 0);
                                Debug.Log("Right");
                            }

                        }
                        if (Distance.x < 0)
                        {
                            if (hit.collider.CompareTag("Player"))
                            {
                                toDrag = hit.transform;
                                toDrag.transform.Translate(-25f * Time.deltaTime, 0, 0);
                                Debug.Log("Left");
                            }
                        }
                    }
                    if (Y_Distance > x_Distance)
                    {
                        if (Distance.y > 0)
                        {
                            if (hit.collider.CompareTag("Player"))
                            {
                                toDrag = hit.transform;
                                toDrag.transform.Translate(0, 0, 25f * Time.deltaTime);
                                Debug.Log("Up");
                            }
                        }
                        if (Distance.y < 0)
                        {
                            if (hit.collider.CompareTag("Player"))
                            {
                                toDrag = hit.transform;
                                toDrag.transform.Translate(0, 0, -25f * Time.deltaTime);
                                Debug.Log("Down");
                            }
                        }
                    }
                }

                #region Old Code - Touch Move
                //v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
                //v3 = Camera.main.ScreenToWorldPoint(v3);
                //toDrag.position = v3   offset;
                #endregion
            }
            if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
            {
                dragging = false;
            }
        }
    }
    #endregion
  • Related