キーボードでの操作

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
joeru
記事: 28
登録日時: 8年前

キーボードでの操作

#1

投稿記事 by joeru » 8年前

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); 
} 
}
 

metaphor

Re: キーボードでの操作

#2

投稿記事 by metaphor » 8年前

フォーラムルールhttp://dixq.net/board/board.htmlを一度お読みください。
4. 義務行為

[C言語何でも質問掲示板でのみ適用される事項]

・トピックを立て、解決した場合は「解決しました」とだけ書かず、どうやって解決したか他の人に分かるように書いてからトピックを終了して下さい。
------

サイトの通りにしたら360C動画・音声とも再生でき”マウスで方向が向けるように”になりました。参考になると思います。

joeru
記事: 28
登録日時: 8年前

Re: キーボードでの操作

#3

投稿記事 by joeru » 8年前

すいません。
マナーがなっていませんでしたね。
最初の質問のほうは、どうやって出来たか書かせていただきます。

joeru
記事: 28
登録日時: 8年前

Re: キーボードでの操作

#4

投稿記事 by joeru » 8年前

かなり下がってしまいましたので上げます

joeru
記事: 28
登録日時: 8年前

Re: キーボードでの操作

#5

投稿記事 by joeru » 8年前

解決しました。
方法としまして、

コード:

 
 transform.Rotate(Vector3.down * Input.GetAxis("Vertical") * Time.deltaTime * speed);
を使用したところスフィアを回転させる事が出来ました。

metaphor

Re: キーボードでの操作

#6

投稿記事 by metaphor » 8年前

あ、忘れてました。良かったですね。”マウスで方向が向けるように”のProgramは英文サイトから上手く取り込めましたか。

joeru
記事: 28
登録日時: 8年前

Re: キーボードでの操作

#7

投稿記事 by joeru » 8年前

すいません。
そのマウスについての記事を発見できず、参考にする事が出来ませんでした・・・

と成りのトトロ

Re: キーボードでの操作

#8

投稿記事 by と成りのトトロ » 8年前

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);
    }
}

joeru
記事: 28
登録日時: 8年前

Re: キーボードでの操作

#9

投稿記事 by joeru » 8年前

返信ありがとうございます!
C#scriptに教えていただきましたプログラミングをそのままコピペし、名前も「MouseLook.cs」にし、main cameraに貼り付けてみたのですが、画像のようにエラーが出ました・・・。
添付ファイル
miss.PNG
miss.PNG (14.1 KiB) 閲覧数: 3165 回

と成りのトトロ

Re: キーボードでの操作

#10

投稿記事 by と成りのトトロ » 8年前

スクリプトをアセットに入れコンパイル後カメラにアタッチhttp://marunouchi-tech.i-studio.co.jp/2237/されましたでしょうか。

閉鎖

“C言語何でも質問掲示板” へ戻る