2015-04-06 63 views
0

我目前正在做一個肖像遊戲,我正試圖實現類似遊戲塗鴉跳躍或激進遊戲的屏幕外移動邏輯。如何在屏幕右側離開遊戲對象時將其重新放置在屏幕左側?

如何將遊戲物體重新定位到屏幕的左側,而不依賴於屏幕大小/長寬比時,屏幕的右側和反之亦然?

這裏的代碼片段,我現在所擁有的:

void CheckBounds() 
    { 
     if (this.transform.position.x < -3.2f) 
     { 
      this.transform.position = new Vector3(3.2f, this.transform.position.y, this.transform.position.z); 
     } 

     if (this.transform.position.x > 3.2f) 
     { 
      this.transform.position = new Vector3(-3.2f, this.transform.position.y, this.transform.position.z); 
     } 
    } 

void MovePlayer() 
{ 
      if (isFacingLeft) 
     { 
      this.transform.position -= new Vector3(speed * Time.deltaTime, 0, 0); 
     } 
     else 
     { 
      this.transform.position += new Vector3(speed * Time.deltaTime, 0, 0); 
     } 
} 

現在這個工作,但它是一個骯髒的解決方案。正如你所看到的,當物體離開屏幕的+/- 3.2f時,我硬編碼了邊界。

這對16:9寬高比設備完美適用,但不適用於其他寬高比設備。

如何檢測屏幕邊緣以使其工作,而不管屏幕寬高比如何?

謝謝

+0

查看本教程,因爲它不使用屏幕邊界之外的任何對象來檢查其位置。 https://youtu.be/VAmlb913-jc – Savlon 2015-04-08 01:25:50

回答

0

我更喜歡放置兩個空標記左右限制。

如果角色超出遊戲邊界(左邊限制或右邊限制),則可以檢查更新(或只是當角色移動以提高性能時)並相應地修復其位置:

喜歡的東西:

if(character.position.x < leftLimit.position.x - MARGIN) { 
    character.position.x = rightLimit.position.x - MARGIN; 
} 

if(character.position.x > rightLimit.position.x + MARGIN) { 
    character.position.x = leftLimit.position.x + MARGIN; 
} 

MARGIN增加了一個小的公差,以避免閃爍。

+0

感謝您的回答。我瞭解你的方法,但是如何將2個空物體放置在屏幕的左/右邊緣而不依賴於設備的屏幕尺寸?你知道,沒有目光。對Unity來說還是新手,對不起。 – aresz 2015-04-06 22:52:46

+0

我習慣於SpriteKit中的遊戲編程。在那裏,我只需使用0來獲得左邊緣和size.width以獲得正確的邊緣。 Unity中有類似的東西嗎? – aresz 2015-04-07 02:14:44

+0

也許使用'相機'來獲得世界點數。看看這個:http://answers.unity3d.com/questions/537081/how-to-make-the-player-stay-within-screen-in-a-sim.html – 2015-04-07 08:08:32

0

此腳本用於在屏幕邊界(視口)內包裝對象的屏幕。你所做的就是把這個腳本放到你的遊戲對象上,剩下的就完成了。沒有必要在你的相機外面碰撞器,它只是放置和玩耍。您還可以設置公共布爾值來確定是否希望您的遊戲對象在寬度,高度或兩者上進行換行。這裏的視頻教程:https://youtu.be/VAmlb913-jc

針對一堆Unity3D教程轉向this website

using UnityEngine; 
using System.Collections; 

public class ScreenWrap : MonoBehaviour 
{ 
    public bool wrapWidth = true; 
    public bool wrapHeight = true; 

    private Renderer _renderer; 
    private Transform _transform; 
    private Camera _camera; 
    private Vector2 _viewportPosition; 
    private bool isWrappingWidth; 
    private bool isWrappingHeight; 
    private Vector2 _newPosition; 

    void Start() 
    { 
     _renderer = GetComponent <Renderer>(); 
     _transform = transform; 
     _camera = Camera.main; 
     _viewportPosition = Vector2.zero; 
     isWrappingWidth = false; 
     isWrappingHeight = false; 
     _newPosition = _transform.position; 
    } 

    void LateUpdate() 
    { 
     Wrap(); 
    } 

    private void Wrap() 
    { 
     bool isVisible = IsBeingRendered(); 

     if (isVisible) 
     { 
      isWrappingWidth = false; 
      isWrappingHeight = false; 
     } 

     _newPosition = _transform.position; 
     _viewportPosition = _camera.WorldToViewportPoint (_newPosition); 

     if (wrapWidth) 
     { 
      if (!isWrappingWidth) 
      { 
       if (_viewportPosition.x > 1) 
       { 
        _newPosition.x = _camera.ViewportToWorldPoint (Vector2.zero).x; 
        isWrappingWidth = true; 
       } 
       else if (_viewportPosition.x < 0) 
       { 
        _newPosition.x = _camera.ViewportToWorldPoint (Vector2.one).x; 
        isWrappingWidth = true; 
       } 
      } 
     } 

     if (wrapHeight) 
     { 
      if (!isWrappingHeight) 
      { 
       if (_viewportPosition.y > 1) 
       { 
        _newPosition.y = _camera.ViewportToWorldPoint (Vector2.zero).y; 
        isWrappingHeight = true; 
       } 
       else if (_viewportPosition.y < 0) 
       { 
        _newPosition.y = _camera.ViewportToWorldPoint (Vector2.one).y; 
        isWrappingHeight = true; 
       } 
      } 
     } 

     _transform.position = _newPosition; 
    } 

    private bool IsBeingRendered() 
    { 
     if (_renderer.isVisible) 
      return true; 
     return false; 
    } 
} 
1

我做了2個資產來解決這個問題:

對象邊線2D &相機邊線

他們讓你簡單地寫:

if(player.GetLeftPosition().x > myCamera.GetRightPosition(10)) 

    player.MoveRightPosition(myCamera.GetLeftPosition(10)); 
  • 他們的工作與任何遊戲對象的比例,精靈的樞軸,對撞機的大小或偏移量

資產鏈接:

物體邊緣2D:http://u3d.as/wkF

相機邊緣:http://u3d.as/xxB

希望你它可以幫助你&節省您的時間。

相關問題