简体   繁体   English

在Unity中旋转2D世界

[英]Rotating 2D world within Unity

I am looking at developing a game within Unity by using C# The game will take the gyroscope orientation and rotate accordingly to the direction of which the phone is rotated. 我正在考虑使用C#在Unity中开发游戏。该游戏将采用陀螺仪方向,并相应于手机旋转的方向旋转。

I am curious of how this would work, I understand how I would be able to read and update the gyroscope orientation however I am unsure on how to assign this to a world to rotate. 我很好奇这是如何工作的,我知道我将如何读取和更新陀螺仪的方向,但是我不确定如何将其分配给旋转的世界。 There will be a player on world which will be the next challenge to prevent the player clipping through the world when it is rotated. 世界上将会有一个玩家,这将是下一个挑战,以防止玩家旋转时穿越世界。 Hence the world should rotate around the players current location. 因此,世界应该围绕玩家当前位置旋转。

I currently have no code as I am in the process of designing this, however i am unable to get the logic of how to make this work within my head 我目前没有任何代码,因为我正在设计中,但是我无法理解如何实现此目的的逻辑

Thankyou 谢谢

如果我的目标正确,我猜想像在“拳头游戏”中那样旋转摄像机就足够了,并且比在保持玩家静止的情况下旋转整个世界要简单得多。

This is how I would go about it, first I wouldn't rotate the world, as that is alot of objects to rotate, but if you really wanted to you could parent all of the world to a single game object, then rotate that object about an axis based off of the players position(See this: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html ). 这就是我要做的事情,首先,我不会旋转世界,因为这是很多要旋转的对象,但是如果您真的想,可以将整个世界作为一个游戏对象的父对象,然后旋转该对象关于基于玩家位置的轴(请参阅: https : //docs.unity3d.com/ScriptReference/Transform.RotateAround.html )。

The simplest way is to attach your main camera to your player(Drag and drop it on your Player Object in your hierchy), then rotate your player. 最简单的方法是将主摄像机连接到播放器(将其拖放到播放器中的播放器对象上),然后旋转播放器。

for an example you can test on your machine without the gyroscrope: 例如,您可以在没有陀螺仪的情况下在计算机上进行测试:

using UnityEngine;

public class rotatorScript: MonoBehaviour {
    void Update() {
        // Will Rotate based off of left/right arrows
        float rotator = -Input.GetAxisRaw("Horizontal");
        // Move up or down based off of up/down Arrows
        float verticalDirection = Input.GetAxisRaw("Vertical");
        // Do the actual movement... using space.self so it is based on the player not the world.
        transform.Translate(Vector3.up * verticalDirection * Time.deltaTime * 5f, Space.self);
        transform.Rotate(0f,0f,90f * Time.deltaTime * rotator); 
    }
}

This script would be attached to the player. 该脚本将附加到播放器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM