当前位置:网站首页>Unity2D--人物移动并转身
Unity2D--人物移动并转身
2022-07-04 04:52:00 【xuzhongzheng123】
在Unity中控制人物移动和转身功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerContral : MonoBehaviour
{
Animator animator;
//判断是否面朝右面
public bool facingRight = true;
//移动速度
public float runSpeed = 5;
Rigidbody2D rigidbody;
Quaternion _rotation;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
_rotation=transform.rotation;
}
void Update()
{
//走路动画的播放
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
animator.SetBool("walk", true);
}
else {
animator.SetBool("walk",false); }
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//设置移动
rigidbody.velocity=new Vector2 (h*runSpeed,v*runSpeed);
//设置转身功能
if (h > 0 && !facingRight) {
Filp(); }
else if (h < 0 && facingRight) {
Filp(); }
}
public void Shuchu() {
}
void Filp() {
facingRight = !facingRight;
//Vector3 theScale = transform.localScale;
//theScale.x *= -1;
//transform.localScale = theScale;
_rotation.y += 180;
if (_rotation.y >= 360) {
_rotation.y -= 360; }
transform.rotation = _rotation;
}
}
通过以上的代码可以实现人物的走路动画和移动转身
边栏推荐
- COMP1721 Creating Classes
- Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
- Unity is connected to the weather system
- 【MATLAB】通信信号调制通用函数 — 插值函数
- 2022广东省赛——编码信息获取 解析flag
- 中科磐云—模块A 基础设施设置与安全加固 评分标准
- Headache delayed double deletion
- Remote desktop client RDP
- Capturing and sorting out external Fiddler -- Conversation bar and filter
- Public inputs in appliedzkp zkevm (13)
猜你喜欢
NTFS 安全权限
数据标注是一块肥肉,盯上这块肉的不止中国丨曼孚科技
2022危险化学品经营单位安全管理人员上岗证题库及答案
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
Utiliser des unités de mesure dans votre code pour une vie meilleure
VSCode的有用插件
Trie number dictionary tree
Just do it with your hands 7 - * project construction details 2 - hook configuration
Zhongke panyun-d module analysis and scoring standard
Technology Management - learning / practice
随机推荐
June 2022 summary
Notepad++--显示相关的配置
Simulated small root pile
中職組網絡安全—內存取證
Zkevm (12) state proof of appliedzkp
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
[matlab] matlab simulation of modulation system - power spectrum and coherent demodulation of AM modulated signal
【MATLAB】MATLAB 仿真数字基带传输系统 — 数字基带传输系统
VSCode的有用插件
在代碼中使用度量單比特,從而生活更美好
全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
【MATLAB】MATLAB 仿真 — 模拟调制系统 之 AM 调制过程
小程序毕业设计---美食、菜谱小程序
STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管
cmake
IP时代来临,电竞酒店如何借好游戏的“东风”?
Customize a pager needed in your project
【MATLAB】MATLAB 仿真 — 窄带高斯白噪声
企业级日志分析系统ELK(如果事与愿违那一定另有安排)
力扣 第 300 场周赛