Unity学习1 控制物体移动

来源:互联网 发布:完全开源的cms系统 编辑:程序博客网 时间:2024/06/06 09:18

2017年9月1日 接触unity 以下为一些学习内容的记录


通过角色控制器来移动物体

对物体Add Component ->Character Controller(角色控制器组件)


新建控制移动的代码

using UnityEngine;
using System.Collections;


public class themove : MonoBehaviour {

private CharacterController controller;//定义角色控制器
public float speed;//速度
public float jumpSpeed=1;//跳跃的速度
// Use this for initialization
void Start () {
controller = this.GetComponent<CharacterController> ();//在初始化的时候找到该场景物体的角色控制器
}

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

Vector3 up = transform.TransformDirection (Vector3.up);//获得up方向的矢量


if (Input.GetKey (KeyCode.Space)) {
controller.Move(up*0.5);//如果按下空格键,通过Move函数来控制跳跃

}
controller.SimpleMove (new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")));//通过角色控制器的SimpleMove函数控制水平方向的移动
}
}


Move函数可以控制各个方向的移动

而SimpleMove函数只能控制水平方向的移动

原创粉丝点击