【问题标题】:How to move the player by tilting the android phone?如何通过倾斜安卓手机来移动播放器?
【发布时间】:2017-05-06 09:18:12
【问题描述】:

我正在尝试移动我的播放器,它只是一个立方体。这些动作应该可以向前、向后、向左和向右移动。当我向左或向右移动时,我仍然会向前看而不是旋转。问题是我无法理解如何通过使用AccelerometerInput 让我的玩家不通过物理墙。我正在尝试制作一些简单的 3d 游戏。因此,如果有人能告诉我这是如何工作的,我将不胜感激。这是代码:

public float speed = 10.0F;

 void FixedUpdate()
    {
        Vector3 dir = Vector3.zero; 
        dir.x = -Input.acceleration.y;
        dir.z = -Input.acceleration.x;
        if (dir.sqrMagnitude > 1) 
            dir.Normalize();

        dir *= Time.deltaTime; 
        transform.Translate(dir * speed); 
     }

我在网上搜索了一些,但不明白要使用什么。有人说Input.GetAxis(“水平”)工作,但我不明白我的播放器将如何通过倾斜安卓手机移动。

【问题讨论】:

    标签: c# unity3d unity5


    【解决方案1】:

    所以一般手机都有游戏方向,z-axis 是深度,y-axis 是高度,x-axis 是宽度。 因为您不希望您的对象继续前进y-axis。您只需将输入用于 x 和 z 轴。
    试试这个(用刚体把它连接到你的播放器上):

    using UnityEngine;
    using System.Collections;
    
    public class playerControl : MonoBehaviour {
    
        private Rigidbody rb;
        public float speed;
    
        void Start()    //first Frame
        {
            rb = GetComponent<Rigidbody>();
        }
    
        void FixedUpdate()
        {
            //physics calc of object
            float moveX = Input.GetAxis("Horizontal");
            float moveZ = Input.GetAxis("Vertical");
    
            Vector3 movement = new Vector3(moveX, 0.0f, moveZ);
    
            rb.AddForce(movement * speed);
        }
    

    了解水平轴和垂直轴是如何实现的check this

    【讨论】:

    • 感谢您的回答。但就像我之前说的那样,我无法让它工作,因为我的播放器没有使用该代码移动。我知道这可能很容易修复,但我无法得到它。我应该更改 InputManager (unity) 中的某些内容吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多