★マウス操作でプレーヤーを動かす

マウスで動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 0.2f;
private Vector3 pos;
void Update()
{
// ★改良(下記の2行をコメントアウトする)
//float moveH = -Input.GetAxis ("Horizontal") * moveSpeed;
//float moveV = -Input.GetAxis ("Vertical") * moveSpeed;
// ★改良(下記の2行を追加する)
float moveH = -Input.GetAxis("Mouse X") * moveSpeed;
float moveV = -Input.GetAxis("Mouse Y") * moveSpeed;
transform.Translate(moveH, 0, moveV);
Clamp();
}
void Clamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
}
Danmaku Ⅱ(基礎2/全24回)
他のコースを見る
マウスで動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 0.2f;
private Vector3 pos;
void Update()
{
// ★改良(下記の2行をコメントアウトする)
//float moveH = -Input.GetAxis ("Horizontal") * moveSpeed;
//float moveV = -Input.GetAxis ("Vertical") * moveSpeed;
// ★改良(下記の2行を追加する)
float moveH = -Input.GetAxis("Mouse X") * moveSpeed;
float moveV = -Input.GetAxis("Mouse Y") * moveSpeed;
transform.Translate(moveH, 0, moveV);
Clamp();
}
void Clamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
}
★マウス操作でプレーヤーを動かす