unity3d中关于控制小车的移动和旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveTest : MonoBehaviour
{
public float speed;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    //按A键向左转头
    if (Input.GetKey(KeyCode.A))
    {
        // transform.position += -transform.right * speed * Time.deltaTime;
        transform.Rotate(-Vector3.up * 50 * Time.deltaTime);
    }
    //按D键向右转头
    if (Input.GetKey(KeyCode.D))
    {
        // transform.position += transform.right * speed * Time.deltaTime;
        transform.Rotate(Vector3.up * 50 * Time.deltaTime);
    }
    //按w键向前移动
     if (Input.GetKey(KeyCode.W))
    {
        transform.position += transform.forward * speed * Time.deltaTime;
    }
    if (Input.GetKey(KeyCode.S))
    {
        transform.position += -transform.forward * speed * Time.deltaTime;
    }
    if (Input.GetKey(KeyCode.Space))
    {
        transform.position += transform.up * speed * Time.deltaTime;
    }


    //鼠标左键旋转
    /*   if (Input.GetKey(KeyCode.Mouse0))
       {
           transform.Rotate(-Vector3.up * Time.deltaTime * 20);
       }
       //鼠标右键旋转
       if (Input.GetKey(KeyCode.Mouse1))
       {
           transform.Rotate(Vector3.up * 20 * Time.deltaTime);
       }*/
}

}

自动移动用transform.Translate
//使小球按照y轴正方向以每帧1的单位长度移动
transform.Translate(Vector3.up * Time.deltaTime, Space.World);

控制物体的旋转以及移动
unity3d中关于控制小车的移动和旋转_第1张图片

你可能感兴趣的:(unity)