当前位置:网站首页>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
边栏推荐
- Character types of C language
- Li Kou's 300th weekly match
- [matlab] communication signal modulation general function - low pass filter
- 令人头痛的延时双删
- Unity 接入天气系统
- 记几个智能手表相关芯片 蓝牙芯片 低功耗
- 【MATLAB】MATLAB 仿真数字带通传输系统 — ASK、 PSK、 FSK 系统
- Automated testing selenium foundation -- webdriverapi
- Test cs4344 stereo DA converter
- Download kicad on Alibaba cloud image station
猜你喜欢

How to build your own knowledge engine? Community open application

Test cs4344 stereo DA converter

2022广东省赛——编码信息获取 解析flag

VSCode的有用插件

Simulink与Arduino串口通信

Graduation design of small programs -- small programs of food and recipes

Trie number dictionary tree

KMP匹配字符串

海力士EMMC5.0及5.1系列对比详解

Download kicad on Alibaba cloud image station
随机推荐
【MATLAB】MATLAB 仿真模拟调制系统 — SSB 系统
如何构建属于自己的知识引擎?社群开放申请
Li Kou's 300th weekly match
Just do it with your hands 7 - * project construction details 2 - hook configuration
How to build your own knowledge engine? Community open application
【无标题】
Remote desktop client RDP
flink1.13 sql基础语法(一)DDL、DML
[matlab] communication signal modulation general function interpolation function
[matlab] matlab simulation - simulate the AM modulation process of the modulation system
2022 t elevator repair operation certificate examination question bank and simulation examination
EVM proof in appliedzkp zkevm (11)
Test cs4344 stereo DA converter
laravel 中获取刚刚插入的记录的id
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
2022危险化学品经营单位安全管理人员上岗证题库及答案
Flutter ‘/usr/lib/libswiftCore. dylib‘ (no such file)
COMP1721 Creating Classes
Trie数-字典树