博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D 之3D游戏角色控制器运动
阅读量:7228 次
发布时间:2019-06-29

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

3D运动,绑定了人形控制器后的一个简单的运动方法。

using UnityEngine;using System.Collections;public class PlayerMove : MonoBehaviour {    private CharacterController cc;    private Animator animator;    public float speed = 4;    void Awake() {        cc = this.GetComponent < CharacterController>();        animator = this.GetComponent
(); } // Update is called once per frame void Update () { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); //按键的取值,以虚拟杆中的值为优先 if (Joystick.h != 0 || Joystick.v != 0) { h = Joystick.h; v = Joystick.v; } if (Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1) { animator.SetBool("Walk", true); if (animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerRun")) { Vector3 targetDir = new Vector3(h, 0, v); transform.LookAt(targetDir + transform.position); cc.SimpleMove(transform.forward * speed); } } else { animator.SetBool("Walk", false); } }}

 复杂的运动,比如尝试着通过动力移动控制器。

public float speed = 6.0F;    public float jumpSpeed = 8.0F;    public float gravity = 20.0F;    private Vector3 moveDirection = Vector3.zero;        //一个复杂的运动        if (cc.isGrounded)        {            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));            moveDirection = transform.TransformDirection(moveDirection);            moveDirection *= speed;            if (Input.GetButton("Jump"))                moveDirection.y = jumpSpeed;        }        moveDirection.y -= gravity * Time.deltaTime;        cc.Move(moveDirection * Time.deltaTime);

可以完成人物贴在地面上运动的效果。

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

你可能感兴趣的文章
android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
查看>>
iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
查看>>
诡异!React stopPropagation失灵
查看>>
Python_OOP
查看>>
个人博客开发系列:评论功能之GitHub账号OAuth授权
查看>>
mongodb--安装和初步使用教程
查看>>
ES6简单总结(搭配简单的讲解和小案例)
查看>>
text-decoration与color属性
查看>>
如何使用Mybatis第三方插件--PageHelper实现分页操作
查看>>
PyCharm搭建GO开发环境(GO语言学习第1课)
查看>>
Android交互
查看>>
提醒我喝水chrome插件开发指南
查看>>
列表数据转树形数据
查看>>
Java新版本的开发已正式进入轨道,版本号18.3
查看>>
从零开始的webpack生活-0x009:FilesLoader装载文件
查看>>
在electron中实现跨域请求,无需更改服务器端设置
查看>>
gitlab-ci配置详解(一)
查看>>
听说你叫Java(二)–Servlet请求
查看>>
案例分享〡三拾众筹持续交付开发流程支撑创新业务
查看>>
FreeWheel业务系统微服务化过程经验分享
查看>>