Blogger Information
Blog 95
fans 0
comment 11
visits 248850
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Unity3d键盘按键控制物体移动、旋转
龍__遇见彩虹的博客
Original
10966 people have browsed it

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : MonoBehaviour {

    private Transform m_Transform;

void Start () {

        m_Transform = gameObject.GetComponent<Transform>();

}

// Update is called once per frame

void Update () {

        MoveControl();

}

    void MoveControl()

    {

        if (Input.GetKey(KeyCode.W))

        {

            m_Transform.Translate(Vector3.forward * 0.1f, Space.Self);

        }

        if (Input.GetKey(KeyCode.S))

        {

            m_Transform.Translate(Vector3.back * 0.1f, Space.Self);

        }

        if (Input.GetKey(KeyCode.A))

        {

            m_Transform.Translate(Vector3.left * 0.1f, Space.Self);

        }

        if (Input.GetKey(KeyCode.D))

        {

            m_Transform.Translate(Vector3.right * 0.1f, Space.Self);

        }

        if (Input.GetKey(KeyCode.Q))

        {

            m_Transform.Rotate(Vector3.up, -1.0f);

        }

        if (Input.GetKey(KeyCode.E))

        {

            m_Transform.Rotate(Vector3.up, 1.0f);

        }

        m_Transform.Rotate(Vector3.up, Input.GetAxis("Mouse X"));

        m_Transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y"));

    }

}



回到Unity3D,点击"运行"按钮,发现原来的Scene面板会切换到Game(游戏预览)面板。这时,就可以通过WSAD键控制立方体的前后左右移动,Q和E键控制立方体左右旋转。同时,鼠标前后和左右移动可以控制物体上下及左右旋转了。


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post