Unity中C#代码学习用wasd和上下左右键控制物体前后左右上下移动和绕轴旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wasd上下 : MonoBehaviour
{
public GameObject chedao;
public GameObject dizuo;
public GameObject zhuanpan;
public GameObject center;
public GameObject shangti;
public float speed = 0.1f;
public float Angels=1;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
{ chedao.transform.localPosition = new Vector3(chedao.transform.localPosition.x , chedao.transform.localPosition.y + Time.deltaTime * speed, chedao.transform.localPosition.z); }
if (Input.GetKey(KeyCode.S))
{ chedao.transform.localPosition = new Vector3(chedao.transform.localPosition.x, chedao.transform.localPosition.y - Time.deltaTime * speed, chedao.transform.localPosition.z); }
if (Input.GetKey(KeyCode.A))
{ dizuo.transform.localPosition = new Vector3(dizuo.transform.localPosition.x- Time.deltaTime * speed,dizuo.transform.localPosition.y,dizuo.transform.localPosition.z); }
if (Input.GetKey(KeyCode.D))
{dizuo.transform.localPosition = new Vector3(dizuo.transform.localPosition.x + Time.deltaTime * speed, dizuo.transform.localPosition.y, dizuo.transform.localPosition.z); }
if (Input.GetKey(KeyCode.LeftArrow))
{ zhuanpan.transform.RotateAround(center.transform.position, Vector3.up, Angels * Time.deltaTime); }
if (Input.GetKey(KeyCode.RightArrow))
{ zhuanpan.transform.RotateAround(center.transform.position, Vector3.down, Angels * Time.deltaTime); }
if (Input.GetKey(KeyCode.UpArrow))
{ shangti.transform.localPosition = new Vector3(shangti.transform.localPosition.x, shangti.transform.localPosition.y , shangti.transform.localPosition.z - Time.deltaTime * speed); }
if (Input.GetKey(KeyCode.DownArrow))
{ shangti.transform.localPosition = new Vector3(shangti.transform.localPosition.x, shangti.transform.localPosition.y , shangti.transform.localPosition.z + Time.deltaTime * speed); }
}
}
|