移动:

  1、SimpleMove(Vector3: vector3&speed)

    简单移动,可以根据vector3方向移动,物体不需要添加刚体即受重力影响,不需要添加碰撞器即可以产生碰撞,但无法推动其它物体。

  2、Move(Vector3: vector3&speed)

    移动,根据vector3方向移动,速度比SimpleMove快许多,不受重力影响,但可以在不添加碰撞器的情况下产生碰撞,无法推动其它物体。

sample

using UnityEngine;
using System.Collections; public class CharControl : MonoBehaviour { CharacterController charCtl ;
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;
} // Update is called once per frame
void Update () {
charCtl.Move(new Vector3(Input.GetAxis("Horizontal"), , Input.GetAxis("Vertical"))* 0.5f) ;
// charCtl.SimpleMove(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))* 10) ;
}
}

碰撞:

  固定代码,需要添加刚体,也可以不添加刚体,改变函数中判断

    public float pushPower = 2.0F;
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;//没有刚体返回空
if (body == null || body.isKinematic)
return; if (hit.moveDirection.y < -0.3F)//被碰撞物体在它下面
return; Vector3 pushDir = new Vector3(hit.moveDirection.x, , hit.moveDirection.z);
body.velocity = pushDir * pushPower;
}

使一人物围绕三个点自动巡逻  转弯时为平滑转弯

两种方法:

  1、 改变transform.forward

  2、 改变transform.rotation

using UnityEngine;
using System.Collections; public class CharControl : MonoBehaviour { CharacterController charCtl ;
public Transform[] point ;
public Transform nextPoint ;
Transform t1 ;
public int index ;
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;
index = ;
nextPoint = point[] ;
} // Update is called once per frame void Update () { // charCtl.Move(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))* 0.5f) ;
// charCtl.SimpleMove(new Vector3(0, 0, Input.GetAxis("Vertical"))* 10) ; // changeForward() ;
changeRotation() ;
}
void changeForward(){
if(Vector3.Distance(IgnoreY(transform.position), IgnoreY(nextPoint.position))>0.2f){
Vector3 direction = (IgnoreY(nextPoint.position)-IgnoreY(transform.position)).normalized ;//must be normalized
transform.forward = Vector3.Lerp(transform.forward, direction, 0.1f);
// charCtl.transform.forward = direction;
// charCtl.transform.LookAt(nextPoint.position);
charCtl.SimpleMove(transform.forward*) ;
}else{
index = (index+)%point.Length ;
nextPoint = point[index] ;
}
}
void changeRotation(){
if(Vector3.Distance(IgnoreY(transform.position), IgnoreY(nextPoint.position))>0.2f){
Vector3 direction = (IgnoreY(nextPoint.position)-IgnoreY(transform.position)).normalized ;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, 0.1f);
charCtl.SimpleMove(transform.forward*) ; }else{
index = (index+)%point.Length ;
nextPoint = point[index] ; }
} Vector3 IgnoreY(Vector3 x){
return new Vector3(x.x, , x.z) ;
}
public float pushPower = 2.0F;
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;//no rigidbody return null
if (body == null || body.isKinematic)
return; if (hit.moveDirection.y < -0.3F)
return; Vector3 pushDir = new Vector3(hit.moveDirection.x, , hit.moveDirection.z);
body.velocity = pushDir * pushPower;
} }

角色控制器(CharacterController)的更多相关文章

  1. U3D组件------CharacterController(角色控制器)

    角色控制器中有碰撞体和刚体的属性 Slope Limit:角色能爬的斜坡的坡度限制 Step Offset:角色走台阶的高度 Skin Width:当场景里面出现多个角色控制器的时候,两个对象在接触的 ...

  2. Unity手游之路<七>角色控制器

    Unity手游之路<七>角色控制器 我们要控制角色的移动,可以全部细节都由自己来实现.控制角色模型的移动,同时移动摄影机,改变视角.当然Unity也提供了一些组件,可以让我们做更少的工作, ...

  3. Unity手游之路&lt;七&gt;角色控制器

    我们要控制角色的移动,能够所有细节都由自己来实现.控制角色模型的移动,同一时候移动摄影机,改变视角.当然Unity也提供了一些组件,能够让我们做更少的工作,实现我们所期望的功能.今天我们就一起系统来学 ...

  4. unity3d-代码控制游戏角色控制器移动

    先上一个gif看看效果.因为图片大小限制.所以录制的比较小.个人认为效果比较牵强.特别是里面的逻辑代码. 不过我还是认为一切是为了先实现,因为我是刚接触的新手. 工程结构图 这次实现的效果是: 1:摄 ...

  5. Unity3D笔记 英保通六 角色控制器

    一.角色控制器 U3D有两种角色控制方式:Rigidbody刚体.角色控制器组件(胶囊体组件) 面试的题目中经常会遇到这个问题: CharacterController和Rigidbody的区别? 这 ...

  6. unity3d角色控制器01

    参考出处貌似是雨松大神.如有侵权,立即删除. 需要导入包 ①将FirstPerson Controller拖拽入Hierarchy(层次视图)中.由于角色控制器是具有一定物理引擎的,所以一定要将它放在 ...

  7. [原]Unity3D深入浅出 - 角色控制器(Character Controller)

    角色控制器主要用于第一人称和第三人称主角的控制,并不使用刚体物理效果. 添加角色控制器的方法:依次打开菜单栏中的Component - Physiscs - Character Controller ...

  8. 【Unity 3D】学习笔记三十八:角色控制器

    角色控制器 在unity中,已经帮我们实现的上下左右跳等动作,并将他们封装成了角色控制器.角色控制器保存在unity标准资源包中,能够说是很的强大.能够模拟第一或者第三人称视角.不受刚体的限制,很适用 ...

  9. 【Unity】11.1 角色控制器 (Character Controller)

    分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 角色控制器(Character Controller)主要用于对第三人称或第一人称游戏主角的控制.如果要创建类人角色,可 ...

随机推荐

  1. sqlite3里类似top的用法

    sqlite3里类似top的用法 在sqlserver中使用top是很正常的,类似这样的语句: SELECT TOP 10 * FROM [index] ORDER BY id DESC; 但是很不幸 ...

  2. 飞翔的圆(Flappy Bird)游戏源码

    这个源码是一个不错的休闲类的游戏源码,飞翔的圆(Flappy Bird)游戏源码V1.0,本项目是一个仿Flappy Bird的小游戏,只不过是把Flappy Bird里面的鸟替换成了简单的圆.感兴趣 ...

  3. Vue.js学习 Item4 -- 数据双向绑定

    Vue.js 的模板是基于 DOM 实现的.这意味着所有的 Vue.js 模板都是可解析的有效的 HTML,且通过一些特殊的特性做了增强.Vue 模板因而从根本上不同于基于字符串的模板,请记住这点. ...

  4. 在xml中添加array

    在values建立arrays(名字可自定义)的xml: <?xml version="1.0" encoding="utf-8"?> <re ...

  5. UCOS2_STM32F1移植详细过程(二)

    Ⅰ.概述 打开上一篇文章新建的工程,是提取的ST标准库里面源代码文件和UCOS工程包源代码文件.下载过的朋友可能会知道,直接编译那个工程会有大片的错误和警告,原因在于那个工程是没有经过修改源代码的工程 ...

  6. 对java面试文章的技术漫谈的C#技术理解

    .NET人技术太菜的话,要好好学习啊,所以看到Java届的面试对话文章,不经意想用C#的知识想做一些回应(其实也没有什么了不起的). 楼下知识文章扩展一览,外加自己接触到的扩展.水太深! static ...

  7. mysql基本知识---20151127-2

    12.日期计算 YEAR( ).MONTH( )和DAYOFMONTH( ).CURDATE().RIGHT() 1>mysql> SELECT name, birth, CURDATE( ...

  8. Factory Girl使用

    1.使用Rspec,详见http://www.cnblogs.com/fanxiaopeng/p/3563772.html 2.在gemfile中添加 #Gemfile group :developm ...

  9. How to create QTP Shared Object Repository

    How to create QTP Shared Object Repository To create a shared object repository by performing follow ...

  10. Android:ViewPager实现屏幕轮转和使用PagerTabStrip

    ① ViewPager类直接继承了ViewGroup类,所有它是一个容器类,可以在其中添加其他的view类. ② ViewPager类需要一个PagerAdapter适配器类给它提供数据. ③ Vie ...