ページ 1 / 1
キーボードでの操作
Posted: 2016年12月22日(木) 12:22
by joeru
unity内に表示されているスフィア(sphere01)をキーボードの方向キーを使って動かしたいです。
動かし方としては方向キーを押してる間は回って、離すと止まるようにしたいです。
下のようにとりあえず、右に動かせるように書いてみたんですけど、どうにも上手くいきません、どうしたら動くようになりますか?
それと今のこのプログラミングですと、方向キーを押してもまったく反応しません。
コード:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class move : MonoBehaviour
{
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.DpadRight))//キーボード
{
Transform.Rotate(Vector3.right * Time.deltaTime);
}
}
Re: キーボードでの操作
Posted: 2016年12月22日(木) 14:41
by metaphor
フォーラムルール
http://dixq.net/board/board.htmlを一度お読みください。
4. 義務行為
[C言語何でも質問掲示板でのみ適用される事項]
・トピックを立て、解決した場合は「解決しました」とだけ書かず、どうやって解決したか他の人に分かるように書いてからトピックを終了して下さい。
------
サイトの通りにしたら360C動画・音声とも再生でき”マウスで方向が向けるように”になりました。参考になると思います。
Re: キーボードでの操作
Posted: 2016年12月22日(木) 15:40
by joeru
すいません。
マナーがなっていませんでしたね。
最初の質問のほうは、どうやって出来たか書かせていただきます。
Re: キーボードでの操作
Posted: 2017年1月10日(火) 15:36
by joeru
かなり下がってしまいましたので上げます
Re: キーボードでの操作
Posted: 2017年1月10日(火) 16:50
by joeru
解決しました。
方法としまして、
コード:
transform.Rotate(Vector3.down * Input.GetAxis("Vertical") * Time.deltaTime * speed);
を使用したところスフィアを回転させる事が出来ました。
Re: キーボードでの操作
Posted: 2017年1月10日(火) 18:04
by metaphor
あ、忘れてました。良かったですね。”マウスで方向が向けるように”のProgramは英文サイトから上手く取り込めましたか。
Re: キーボードでの操作
Posted: 2017年1月11日(水) 23:37
by joeru
すいません。
そのマウスについての記事を発見できず、参考にする事が出来ませんでした・・・
Re: キーボードでの操作
Posted: 2017年1月12日(木) 00:02
by と成りのトトロ
http://warapuri.com/post/131599525953/u ... 9%E7%B7%A8「ここの」
のマウスで方向が向けるように
MouseLook.csというスクリプトを利用しましょう。
これをダウンロードして、「Camera」オブジェクトにD&Dでいけます。
https://github.com/makoto-unity/Panoram ... useLook.cs
「ここです」
TeraPad の矩形選択で行番号をのぞいたものです。
コード:
/*
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
*/
using UnityEngine;
using System.Collections;
/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character:
/// - Create a capsule.
/// - Add a rigid body to the capsule
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSWalker script to the capsule
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
///
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
void Update()
{
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis(-rotationY, Vector3.right);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start()
{
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
Re: キーボードでの操作
Posted: 2017年1月12日(木) 01:00
by joeru
返信ありがとうございます!
C#scriptに教えていただきましたプログラミングをそのままコピペし、名前も「MouseLook.cs」にし、main cameraに貼り付けてみたのですが、画像のようにエラーが出ました・・・。
Re: キーボードでの操作
Posted: 2017年1月12日(木) 07:53
by と成りのトトロ