当前位置:网站首页>Unity2d -- character moves and turns
Unity2d -- character moves and turns
2022-07-04 05:13:00 【xuzhongzheng123】
stay Unity Control character movement and turning function in
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerContral : MonoBehaviour
{
Animator animator;
// Judge whether it faces the right
public bool facingRight = true;
// Movement speed
public float runSpeed = 5;
Rigidbody2D rigidbody;
Quaternion _rotation;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
_rotation=transform.rotation;
}
void Update()
{
// Playback of walking animation
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");
// Set up mobile
rigidbody.velocity=new Vector2 (h*runSpeed,v*runSpeed);
// Set the turning function
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;
}
}
Through the above code, you can realize the character's walking animation and mobile turning
边栏推荐
- 自动化测试selenium基础篇——webdriverAPI
- Using jsts in esmodule environment
- Detailed comparison of Hynix emmc5.0 and 5.1 series
- 2022广东省赛——编码信息获取 解析flag
- KMP match string
- We believe that the development of consumer Internet will still be limited to the Internet industry itself
- [matlab] communication signal modulation general function interpolation function
- [matlab] matlab simulates digital baseband transmission system - digital baseband transmission system
- [matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
- Several smart watch related chips Bluetooth chip low power consumption
猜你喜欢
Simple g++ and GDB debugging
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
Zhongke panyun-d module analysis and scoring standard
TCP状态转换图
2022年R2移动式压力容器充装复训题库及答案
[QT] timer
Customize a pager needed in your project
Public inputs in appliedzkp zkevm (13)
Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)
中科磐云—2022广东木马信息获取解析
随机推荐
Headache delayed double deletion
加密和解密
空洞卷积、可变形卷积、可变形ROI Pooling
Useful plug-ins for vscode
C basic (VII) document operation
Simulink与Arduino串口通信
[matlab] matlab simulation modulation system - DSB system
练习-冒泡排序
中職組網絡安全—內存取證
简单g++和gdb调试
【MATLAB】通信信号调制通用函数 — 带通滤波器
QT qtableview data column width adaptation
appliedzkp的zkevm(12)State Proof
LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
Using jsts in esmodule environment
TCP state transition diagram
Flutter 调用高德地图APP实现位置搜索、路线规划、逆地理编码
小程序毕业设计---美食、菜谱小程序
2022G2电站锅炉司炉特种作业证考试题库及答案
Annex 2-2 confidentiality commitment docx