如何用键盘按键设置对象位置?

时间:2016-11-05 10:01:42

标签: c# unity3d

HY 我想制作游戏,我想在哪里开始我的迷你高尔夫游戏... 所以,当我探索。按1,然后把我的球带到特定的位置进行探索。至x 5,y 10,z 15

如何在Unity3d中创建int C#?

1 个答案:

答案 0 :(得分:0)

以下是我所制作的代码,在评论中向您解释:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class testscript : MonoBehaviour {

    [SerializeField]
    List<KeyCode> keys; //array also acceptable here, but i prefer to use Lists
    //for line above, this could also be a List<string> because of the other overload of Input.GetKey but this way is best practice

    [SerializeField]
    Transform transform;

    [SerializeField]
    List<Vector3> pos;

    void Update(){
        for (int i = 0; i < keys.Count; i++) { //not foreach because we will need value of i
            if (Input.GetKey (keys [i])) { //this will be continuous, you could also use input.GetKeyDown
                //*****************
                //DO SOMETHING HERE
                //*****************
                transform.position = pos[i];
                Debug.Log ("Yay! A key was pressed!");
            }
        }
    }
}

要回答您的第二个问题,要在C#中int进行int varname;,或者在创建时定义它,您可以执行int varname = numval