Translate と Rotate

確認済のバージョン: 4

-

難易度: 初級

リジッドボディ以外のオブジェクトの位置と回転を制御するために transform 関数の Translate と Rotate の使い方を学びます。

Translate と Rotate

初級 Scripting

TransformFunctions

Code snippet

using UnityEngine;
using System.Collections;

public class TransformFunctions : MonoBehaviour
{
    public float moveSpeed = 10f;
    public float turnSpeed = 50f;
    
    
    void Update ()
    {
        if(Input.GetKey(KeyCode.UpArrow))
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.DownArrow))
            transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.LeftArrow))
            transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.RightArrow))
            transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    }
}
#pragma strict

public var moveSpeed : float = 10f;
public var turnSpeed : float = 50f;


function Update ()
{
    if(Input.GetKey(KeyCode.UpArrow))
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    
    if(Input.GetKey(KeyCode.DownArrow))
        transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
    
    if(Input.GetKey(KeyCode.LeftArrow))
        transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
    
    if(Input.GetKey(KeyCode.RightArrow))
        transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
import UnityEngine
import System.Collections


public class TransformFunctions(MonoBehaviour):

    public moveSpeed as single = 10.0F

    public turnSpeed as single = 50.0F

    
    
    private def Update():
        if Input.GetKey(KeyCode.UpArrow):
            transform.Translate(((Vector3.forward * moveSpeed) * Time.deltaTime))
        
        if Input.GetKey(KeyCode.DownArrow):
            transform.Translate((((-Vector3.forward) * moveSpeed) * Time.deltaTime))
        
        if Input.GetKey(KeyCode.LeftArrow):
            transform.Rotate(Vector3.up, ((-turnSpeed) * Time.deltaTime))
        
        if Input.GetKey(KeyCode.RightArrow):
            transform.Rotate(Vector3.up, (turnSpeed * Time.deltaTime))