Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Game - Change Perspective from Orthographic to Perspective

Discussion in 'Scripting' started by solideuk, Dec 6, 2015.

  1. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41
    Hi All,

    I'm working on a 2D tile based game. I am able to move the tiles/chips around as normal, but now feel that having the world 'appear' 3D would look better.

    What would be the best approach to make the world appear 3D while still constraining the movement to the current 2D plane?

    Code (CSharp):
    1.         if (Input.GetMouseButtonDown(0)) {
    2.             Vector2 point = controlCamera.ScreenPointToRay(Input.mousePosition).origin;
    3.             hit = Physics2D.Raycast(point, Vector2.zero);
    4.             if (!hit.transform) return;
    5.             pressedChip = hit.transform.GetComponent<Chip>();
    6.             pressPoint = Input.mousePosition;
    7.         }
    8.         if (Input.GetMouseButton(0) && pressedChip != null) {
    9.             Vector2 move = Input.mousePosition;
    10.             move -= pressPoint;
    11.             if (move.magnitude > Screen.height * 0.05f) {
    12.                 foreach (Side side in Utils.straightSides)
    13.                     if (Vector2.Angle(move, Utils.SideOffsetX(side) * Vector2.right + Utils.SideOffsetY(side) * Vector2.up) <= 45)
    14.                         pressedChip.Swap(side);
    15.                 pressedChip = null;
    16.             }
    17.         }
    18.  
    19.   public Chip GetChipFromTouch() {
    20.      Vector2 point;
    21.      if (isMobilePlatform) {
    22.        if (Input.touchCount == 0) return null;
    23.        point = controlCamera.ScreenPointToRay(Input.GetTouch(0).position).origin;
    24.      } else
    25.        point = controlCamera.ScreenPointToRay(Input.mousePosition).origin;
    26.      
    27.      hit = Physics2D.Raycast(point, Vector2.zero);
    28.      if (!hit.transform) return null;
    29.      return hit.transform.GetComponent<Chip>();
    30.    }
    31.  
     
    Last edited: Dec 6, 2015
  2. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41
    I've looked into this further, it appears that a 2.5D is the approach I want to take.

    I've changed the camera perspective to Perspective. Though now the mouse clicks are out of synch and they are not activating the right tiles/chips.

    Any suggestions?
     
  3. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41
    Hi

    Apologies for bumping this again.

    Having a difficult time converting this to accept Vector3 as opposed to Vector2, any help would be very greatly appreciated.

    What would be the best way to insert Vector3 functions but still preserve the 2D mechanics of the games?

    Thanks
     
    Last edited: Dec 7, 2015
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You could look at some of the 2.5d assets in the asset store, they might help
     
  5. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41
    Thanks though it's difficult to find an asset that incorporates similar functions of the above.

    I will try using Vector3 raycast to obtain the clicks,
    how about the segment of code below as it is Vector2 do I need to convert the Vector3 readings to Vector2?

    You see I want the 2D game mechanics to work as normal and just change the way the mouse clicks are recognised.

    Code (CSharp):
    1. if (move.magnitude > Screen.height * 0.05f) {
    2.               foreach (Side side in Utils.straightSides)
    3.                   if (Vector2.Angle(move, Utils.SideOffsetX(side) * Vector2.right + Utils.SideOffsetY(side) * Vector2.up) <= 45)
    4.                       pressedChip.Swap(side);
    5.               pressedChip = null;
    6.           }