Move方法与CharacterController才能配合使用

Move方法与CharacterController才能配合使用

transform.Translate()与GameObject配合使用

RepeatButton作用是按键按下后不抬起,则一直执行某动作;

using UnityEngine;
using System.Collections;


public class Scripts6_6 : MonoBehaviour {
private CharacterController col= null;
private float moveSpeed = 3.0f;
private float rotSpeed = 3.0f;
// Use this for initialization
void Start () {
col = this.GetComponent<CharacterController>();
}

// Update is called once per frame
void Update () {

}
void OnGUI(){
if (GUILayout.RepeatButton ("向左转动", GUILayout.Width (80), GUILayout.Height (30))) {
this.transform.Rotate (0, -rotSpeed, 0);
}
if (GUILayout.RepeatButton ("向右旋转", GUILayout.Width (80), GUILayout.Height (30))) {
this.transform.Rotate (0, rotSpeed, 0);
}
if (GUILayout.RepeatButton ("向前移动", GUILayout.Width (80), GUILayout.Height (30))) {
Vector3 forwards = transform.TransformDirection (Vector3.forward);
col.Move (forwards*moveSpeed);
}
if (GUILayout.RepeatButton ("向后移动", GUILayout.Width (80), GUILayout.Height (30))) {
//TransformDirection方法得到当前主角移动时面朝的方向
Vector3 backwards = transform.TransformDirection (Vector3.back);
col.Move (backwards*moveSpeed);
}
if (GUILayout.RepeatButton ("起飞", GUILayout.Width (80), GUILayout.Height (30))) {
this.transform.Translate (0, 1, 0);
}
if (GUILayout.RepeatButton ("降落", GUILayout.Width (80), GUILayout.Height (30))) {
this.transform.Translate (0, -1, 0);
}
}
}

你可能感兴趣的:(C#,脚本,unity)