当前位置:网站首页>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;
}
}
通过以上的代码可以实现人物的走路动画和移动转身
边栏推荐
- Yolov6 practice: teach you to use yolov6 for object detection (with data set)
- Annex 2-2 confidentiality commitment docx
- 自动化测试selenium基础篇——webdriverAPI
- How to build your own knowledge engine? Community open application
- Zhongke panyun-2022 Guangdong Trojan horse information acquisition and analysis
- appliedzkp zkevm(13)中的Public Inputs
- 力扣 第 300 场周赛
- 【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre
- Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
- 【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(余弦滚降成形脉冲)的眼图
猜你喜欢
随机推荐
NTFS security permissions
自动化测试selenium基础篇——webdriverAPI
Capturing and sorting out external Fiddler -- Conversation bar and filter
Encryption and decryption
Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)
[matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
appliedzkp的zkevm(12)State Proof
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
Flutter ‘/usr/lib/libswiftCore. dylib‘ (no such file)
Li Kou's 300th weekly match
[matlab] matlab simulation modulation system SSB system
Detailed comparison of Hynix emmc5.0 and 5.1 series
Zhongke panyun-d module analysis and scoring standard
[paper summary] zero shot semantic segmentation
[technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid
【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre
[matlab] matlab simulation modulation system FM system
Simulink与Arduino串口通信
【MATLAB】通信信号调制通用函数 — 低通滤波器
【MATLAB】通信信号调制通用函数 — 带通滤波器








