Unity 方向控制及鼠标滚轮代码控制

方向控制 JavaScript语言 

#pragma strict
function Start () {
}
var MoveSpeed=5;
var RotateSpeed=20;
function Update () {
if(this.transform.up.y>0 && this.transform.up.y<=10)
 if(Input.GetKey(KeyCode.W))
 {
  print("Moving W");
  if(MoveSpeed<=30){
   MoveSpeed = MoveSpeed + 10*Time.deltaTime;
  }
  this.transform.Translate(Vector3.forward*5*Time.deltaTime*MoveSpeed);
  if(Input.GetKey(KeyCode.A))
  {
   this.transform.Rotate(Vector3.up*5*Time.deltaTime*-RotateSpeed);  
  }
  else if(Input.GetKey(KeyCode.D))
  {
   this.transform.Rotate(Vector3.up*5*Time.deltaTime*RotateSpeed);  
  }
  
 }
 else if(Input.GetKey(KeyCode.S))
 {
  print("MOving S");
  
   MoveSpeed = 5;
  
  this.transform.Translate(Vector3.forward*5*Time.deltaTime*-MoveSpeed);
  if(Input.GetKey(KeyCode.A))
  {
   this.transform.Rotate(Vector3.up*5*Time.deltaTime*-RotateSpeed);  
  }
  else if(Input.GetKey(KeyCode.D))
  {
   this.transform.Rotate(Vector3.up*5*Time.deltaTime*RotateSpeed);  
  }
 }
 else if(Input.GetKey(KeyCode.A))
 {
  MoveSpeed = 5;
  this.transform.Translate(Vector3.forward*5*Time.deltaTime*0.1*MoveSpeed);
  this.transform.Rotate(Vector3.up*5*Time.deltaTime*-RotateSpeed);
 }
 else if(Input.GetKey(KeyCode.D))
 {
  MoveSpeed = 5;
  this.transform.Translate(Vector3.forward*5*Time.deltaTime*0.1*MoveSpeed);
  this.transform.Rotate(Vector3.up*5*Time.deltaTime*RotateSpeed);
 }
}



滚轮控制  C#语言

using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
  if( Input.GetAxis("Mouse ScrollWheel") != 0 )
  {
   this.gameObject.transform.Translate(new Vector3(0,0,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*500));
  }
 }
}



你可能感兴趣的:(unity3D)