私はC#を初めて使っています。私はUnityで何かを作って、C#とUnityをもっとよく学ぶのを助けています。キーが押されている間のC#とUnity3D
私は理由を知りたい:
void Update()
私は(にキーを押している間、それが発射されていない理由更新は、ループであるので:内に置かれたとき
Input.GetKeyDown(KeyCode.UpArrow))
は一度だけ発生させます私の場合は球を動かす)?
私は、キーを押して離したときに変更される2つのboolを使用して動作させることができました。
documentationusing UnityEngine;
using System.Collections;
public class sphereDriver : MonoBehaviour {
int x ;
bool upPressed = false ;
bool downPressed = false ;
void Start()
{
x = 0 ;
}
void Update()
{
if(x > 0) {
x -= 1 ;
}
if(x < 0) {
x += 1 ;
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
upPressed = true ;
}
else if(Input.GetKeyUp(KeyCode.UpArrow))
{
upPressed = false ;
}
if(upPressed == true)
{
x += 5 ;
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
downPressed = true ;
}
else if(Input.GetKeyUp(KeyCode.DownArrow))
{
downPressed = false ;
}
if(downPressed == true)
{
x -= 5 ;
}
transform.Translate(x * Time.deltaTime/10, 0, 0) ;
}
}
ありがとうございました!これらの2つの関数はUnity固有のものなので、元々やったやり方は、もともとC#で欲しいものを達成するためにほとんど何をするのでしょうか? – imperium2335
はい、これは一般的な方法です。 – redtuna