▼このように回転▼

オブジェクトをZ軸を中心にして回転させます。
前提条件
- 回転させたいオブジェクトにスクリプト(名前をPlayerControllerしました)がアタッチされている
だけです。そのスクリプトに書いていってください。
Z軸を中心に回転させるスクリプト
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
    Rigidbody rigidBody;
    // Use this for initialization
void Start()
{
        rigidBody = GetComponent<Rigidbody>();
}
    // Update is called once per frame
void Update()
{
        RotateObject();
}
    void RotateObject()
{
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward);
        }
}
}
| if (Input.GetKey(KeyCode.A)) { //処理 } | Aキーが押されたときに処理を行う | 
| transform.Rotate(Vector3.forward); | z軸を中心に回転させる(Rotationのz軸の値が上がる) | 

このように回転します。RotationのZ軸の値が上がることによって回転させています。
回転するスピードを上げたい場合
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
    Rigidbody rigidBody;
    int speed = 10;
    // Use this for initialization
void Start()
{
        rigidBody = GetComponent<Rigidbody>();
}
    // Update is called once per frame
void Update()
{
        RotateObject();
}
    void RotateObject()
    {
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * speed);
        }
    }
}
Vector3.forwardに数値をかけることによって、回転するスピードがあがります。自分の望むスピードにしてください。
ちなみにこの記事を合わせるとロケットのようなものができるよ

この記事と組み合わせるとロケットのようなものができます。これも記事にする予定です。
まとめ
- transform.Rotate(Vector3.forward);でz軸を中心に回転させることができる
以上さぎのみや(@saginomiya8)でした。


 
	 
	 
		 
		 
			 
			 
			 
			 
			 
			 
			 
			


コメント