角色控制器(CharacterController)
移动:
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)的更多相关文章
- U3D组件------CharacterController(角色控制器)
角色控制器中有碰撞体和刚体的属性 Slope Limit:角色能爬的斜坡的坡度限制 Step Offset:角色走台阶的高度 Skin Width:当场景里面出现多个角色控制器的时候,两个对象在接触的 ...
- Unity手游之路<七>角色控制器
Unity手游之路<七>角色控制器 我们要控制角色的移动,可以全部细节都由自己来实现.控制角色模型的移动,同时移动摄影机,改变视角.当然Unity也提供了一些组件,可以让我们做更少的工作, ...
- Unity手游之路<七>角色控制器
我们要控制角色的移动,能够所有细节都由自己来实现.控制角色模型的移动,同一时候移动摄影机,改变视角.当然Unity也提供了一些组件,能够让我们做更少的工作,实现我们所期望的功能.今天我们就一起系统来学 ...
- unity3d-代码控制游戏角色控制器移动
先上一个gif看看效果.因为图片大小限制.所以录制的比较小.个人认为效果比较牵强.特别是里面的逻辑代码. 不过我还是认为一切是为了先实现,因为我是刚接触的新手. 工程结构图 这次实现的效果是: 1:摄 ...
- Unity3D笔记 英保通六 角色控制器
一.角色控制器 U3D有两种角色控制方式:Rigidbody刚体.角色控制器组件(胶囊体组件) 面试的题目中经常会遇到这个问题: CharacterController和Rigidbody的区别? 这 ...
- unity3d角色控制器01
参考出处貌似是雨松大神.如有侵权,立即删除. 需要导入包 ①将FirstPerson Controller拖拽入Hierarchy(层次视图)中.由于角色控制器是具有一定物理引擎的,所以一定要将它放在 ...
- [原]Unity3D深入浅出 - 角色控制器(Character Controller)
角色控制器主要用于第一人称和第三人称主角的控制,并不使用刚体物理效果. 添加角色控制器的方法:依次打开菜单栏中的Component - Physiscs - Character Controller ...
- 【Unity 3D】学习笔记三十八:角色控制器
角色控制器 在unity中,已经帮我们实现的上下左右跳等动作,并将他们封装成了角色控制器.角色控制器保存在unity标准资源包中,能够说是很的强大.能够模拟第一或者第三人称视角.不受刚体的限制,很适用 ...
- 【Unity】11.1 角色控制器 (Character Controller)
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 角色控制器(Character Controller)主要用于对第三人称或第一人称游戏主角的控制.如果要创建类人角色,可 ...
随机推荐
- [原]Python 简单文件处理
仅仅是为了Linux操作方便= =命令行最近没有时间仔细看看,电脑一直都在机房,暂且这般记着吧= = spath="D:/download/baa.txt" f=open(spat ...
- 开始安装 ASP.NET (4.0.30319.18408)。 出现了错误: 0x8007b799 必须具有此计算机的管理员权限才能运行此工具
在Visual Studio命令提示符安装ASP.NET .出现了错误: 0x8007b799 必须具有此计算机的管理员权限才能运行此工具:如下图: 解决方案如下: 1.打开“C:\Windows\S ...
- 学习c编程的第三天
#include<stdio.h> int add(int,int); int main(){ int x=add(1,2); printf("%d",x); retu ...
- 12)Java Constructor
Constructor 构造器Constructor不能被继承,因此不能重写Overriding,但可以被重载Overloading. 构造器用来确保每个对象都会得到初始化.当对 ...
- ASP.NET MVC5学习笔记之Controller同步执行架构分析
在开始之前,声明一下,由于ASP.NET MVC5正式发布了,后面的分析将基于ASP.NET MVC5最新的源代码.在前面的内容我们分析了怎样根据路由信息来确定Controller的类型,并最终生成C ...
- SQL Server 锁表、查询被锁表、解锁相关语句
SQL Server 锁表.查询被锁表.解锁相关语句,供参考. --锁表(其它事务不能读.更新.删除) BEGIN TRAN SELECT * FROM <表名> WITH(TABLOCK ...
- NuGet 的使用
install-package entityframework//Enable-Migrations -ContextTypeName College.Models.CollegeEntities ...
- a 标签 跳转4种类型
<a href='' target=''>中的target有4种参数: '_self' , '_parent' , '_top' 和 '_blank' 在没有使用框架 ...
- STM32F0xx_GPIO配置详细过程
前言 对于初学STM32的人来说,很多基础的知识没有掌握,这些基础知识就成为阻挡他们入门的门槛.因此,今天也把基础的知识分享出来,带领那些还没有迈过这个门槛的人入门. 今天总结“GPIO配置详细”,以 ...
- 自学Python二 Python中的屠龙刀(续)
函数 秉承着一切皆对象的理念,函数作为对象,可以为其赋值新的对象名,也可以作为参数传递给其他函数! 正常的诸如空函数,默认参数等等我们就不提了,在这里着重提一下默认参数里面的坑和lambda函数. 当 ...