官方文档:https://docs.unity.cn/cn/2019.4/Manual/class-CharacterController.html
直接上代码:
public class PlayerController : MonoBehaviour
{
//获取组件
public CharacterController characterController;
//设置移动和跳跃速度
public float moveSpeed;
//定义按键输入
public float getHorizontal, getVertical;
//定义移动向量
private Vector3 dir;
private void Start()
{
moveSpeed = 4;
characterController = GetComponent<CharacterController>();
}
private void Update()
{
getHorizontal = Input.GetAxis("Horizontal") * moveSpeed;
getVertical = Input.GetAxis("Vertical") * moveSpeed;
dir = transform.forward * getVertical + transform.right * getHorizontal;
characterController.Move(dir * Time.deltaTime);
}
}
public class PlayerController : MonoBehaviour
{
//定义重力和下落加速度
public float gravity = 9.8f;
private Vector3 velocity = Vector3.zero;
//定义bool值判断角色是否在地面上
public bool isGround;
//定义一个目标点,一个检测半径,一个检测图层
public Transform checkGround;
public float checkRedius;
public LayerMask checkLayer;
private void Start()
{
checkRedius = 0.2f;
}
private void Update()
{
isGround = Physics.CheckSphere(checkGround.position, checkRedius, checkLayer);
if (isGround && isJumping < 0.1)
{
velocity.y = 0;
}
velocity.y -= gravity * Time.deltaTime;
characterController.Move(velocity * Time.deltaTime);
}
}
在这里遇到一个困难,如果直接用以下代码的话:
if (Input.GetButtonDown("Jump") && isGround)
{
velocity.y = jumpSpeed;
}
理论上可行,但是通过实践发现根本跳不起来,猜测和在上一步添加重力时的这一段代码有关:
if (isGround && isJumping < 0.1)
{
velocity.y = 0;
}
在起跳的时候程序又执行到这一部分,导致角色的velocity.y=0,从而使角色不能跳跃
暂时想到的解决办法就是添加一个计时器,让在起跳0.1秒内不再执行判断isGround的代码
通过修改后,PlayerController代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//获取组件
public CharacterController characterController;
//设置移动速度
public float moveSpeed;
//定义按键输入
public float getHorizontal, getVertical;
//定义移动向量
private Vector3 dir;
//定义跳跃速度和跳跃状态
public float jumpSpeed;
public float isJumping;
//定义重力和下落加速度
public float gravity = 9.8f;
private Vector3 velocity = Vector3.zero;
//定义bool值判断角色是否在地面上
public bool isGround;
//定义一个目标点,一个检测半径,一个检测图层
public Transform checkGround;
public float checkRedius;
public LayerMask checkLayer;
private void Start()
{
moveSpeed = 4;
jumpSpeed = 5;
checkRedius = 0.2f;
characterController = GetComponent<CharacterController>();
isJumping = 0.2f;
}
private void Update()
{
isGround = Physics.CheckSphere(checkGround.position, checkRedius, checkLayer);
if (isGround && isJumping < 0.1)
{
velocity.y = 0;
}
getHorizontal = Input.GetAxis("Horizontal") * moveSpeed;
getVertical = Input.GetAxis("Vertical") * moveSpeed;
dir = transform.forward * getVertical + transform.right * getHorizontal;
characterController.Move(dir * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGround)
{
velocity.y = jumpSpeed;
isJumping = 0.2f;
}
if(isJumping > 0)
{
isJumping -= Time.deltaTime;
}
velocity.y -= gravity * Time.deltaTime;
characterController.Move(velocity * Time.deltaTime);
}
}
** 如果有关于玩家跳跃的更好解决办法,欢迎发表在评论区 **
前面完成了前后左右移动和跳跃,这一步完成一个第一视角的转动功能
将Camera拖到Player的节点下,调整位置,然后为Camera添加脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform Player;
private float mouseX, mouseY; //获取鼠标移动的值
public float mouseSensitivity; //鼠标灵敏度
private float xRotation;
private void Start()
{
mouseX = 0;
mouseY = 0;
mouseSensitivity = 800;
}
private void Update()
{
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
if (xRotation > 90)
{
xRotation = 90;
}
else if (xRotation < -90)
{
xRotation = -90;
}
Player.Rotate(Vector3.up * mouseX);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
}
}
完成!
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
在我的Character模型中,我添加了:字符.rbbefore_savedoself.profile_picture_url=asset_path('icon.png')end但是,对于数据库中已存在的所有角色,它们的profile_picture_url为nil。因此,我想进入控制台并遍历所有这些并进行设置。在我试过的控制台中:Character.find_eachdo|c|c.profile_picture_url=asset_path('icon.png')end但这给出了错误:NoMethodError:undefinedmethod`asset_path'formain:O
当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question
我正在尝试将$stdout设置为临时写入一个文件,然后返回到一个文件。test.rb:old_stdout=$stdout$stdout.reopen("mytestfile.out",'w+')puts"thisgoesinmytestfile"$stdout=old_stdoutputs"thisshouldbeontheconsole"$stdout.reopen("mytestfile1.out",'w+')puts"thisgoesinmytestfile1:"$stdout=old_stdoutputs"thisshouldbebackontheconsole"这是输出。r