博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
滑板控制器
阅读量:7119 次
发布时间:2019-06-28

本文共 1927 字,大约阅读时间需要 6 分钟。

private var character :CharacterController;

private var speed =0.0;
private var trans:Transform;
private var targetRot:Quaternion;

 var rotationSpeed = 90 ;

 var pedalImpulse = 3.5;
 var maxSpeed = 15;
 var decayRate = 0.1;
 var jumpSpeed : float = 8.0;
 var raycastMask :LayerMask;
 
function Start()
{
   character = GetComponent(CharacterController);
   trans= transform ;
}

function pedal()

{
    speed += pedalImpulse;
    speed = Mathf.Min(speed, maxSpeed);
}

function Update ()
{
   var horizontal = Input.GetAxis("Horizontal");
   var Vertical = Input.GetAxis("Vertical");
   transform.Rotate(0,rotationSpeed*horizontal*Time.deltaTime,0);
  
   if(character.isGrounded)
     pedal();
   //For normalized vectors Dot returns 1 if they point in exactly the same direction, -1
   //if they point in completely opposite directions and zero if the vectors are perpendicular.
   var moveDirection = trans.forward*speed*Vertical;
   var upright = Vector3.Dot(trans.forward,Vector3.up);//upright >0 是向上行走 <0向下行走
   Debug.Log(upright.ToString());
   //we want fake gravity when the character becomes upright
   //become the characterController dosen`t rotate
  
   //当x>0,sign(x)=1;当x=0,sign(x)=0; 当x<0, sign(x)=-1
   //Clamp01  Clamps value between 0 and 1 and returns value.
   moveDirection += Vector3.Lerp( Physics.gravity,Mathf.Sign(upright)
               * -trans.forward * Physics.gravity.magnitude,
               Mathf.Clamp01(Mathf.Abs(upright)));
  
   character.Move(moveDirection*Time.deltaTime);
  
   //旋转角色根据地形法线
   var ray = new Ray(trans.position+Vector3.up,-Vector3.up);
   var hit : RaycastHit;
   if (character.isGrounded && Physics.Raycast(ray,hit,100,raycastMask))
   {
       var targetRight = Vector3.Cross(hit.normal,trans.forward);
       var targetForward = Vector3.Cross(targetRight,hit.normal);
       targetRot = Quaternion.LookRotation(targetForward,hit.normal);
   }
  
   trans.rotation = Quaternion.Slerp(trans.rotation,targetRot,5*Time.deltaTime);
  
   if(character.isGrounded)
   {
       if(speed<0.3)
         speed=0;
    else
    {
       speed -= decayRate*Time.deltaTime*speed;
    }
   }
  
  
}

 

转载地址:http://yhnel.baihongyu.com/

你可能感兴趣的文章
windows应用迁移到linux下
查看>>
linux网卡研究
查看>>
考虑用静态工厂方法代替构造器
查看>>
常见排序算法:冒泡排序
查看>>
我的友情链接
查看>>
使用ZooKeeper
查看>>
Jenkins+git 实现代码自动发布
查看>>
常用工具——Rsync
查看>>
SylixOS 驱动程序加载
查看>>
插入排序
查看>>
4 python 中 关于数值及运算
查看>>
织梦轻量级mvc框架笔记
查看>>
cocos2dx 3.10 网狐土豪金版PC+手机端棋牌平台搭建
查看>>
减小VirtualBox虚拟硬盘文件的大小
查看>>
Hadoop概述
查看>>
Linux系统日志详解
查看>>
Linux笔记(shell特殊符号,sort排序,wc统计,uniq去重,tee,tr,split)
查看>>
11.15PMP试题每日一题
查看>>
华为模拟器如何实现不同Vlan不同网段之间的互通
查看>>
PHP 实现Session入库/存入redis
查看>>