当前位置:网站首页>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
边栏推荐
- Trie数-字典树
- 中科磐云—2022广东木马信息获取解析
- Graduation design of small programs -- small programs of food and recipes
- [matlab] matlab simulation of modulation system - power spectrum and coherent demodulation of AM modulated signal
- [matlab] matlab simulation modulation system - VSB system
- Fault analysis | mongodb 5.0 reports an error, and the legal instruction solves it
- [matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
- Simulated small root pile
- [paper summary] zero shot semantic segmentation
- 【MATLAB】MATLAB 仿真数字带通传输系统 — ASK、 PSK、 FSK 系统
猜你喜欢
随机推荐
Yolov6 practice: teach you to use yolov6 for object detection (with data set)
数据标注是一块肥肉,盯上这块肉的不止中国丨曼孚科技
模拟小根堆
如何使用postman实现简单的接口关联【增删改查】
cmake
【MATLAB】通信信号调制通用函数 — 窄带高斯白噪声的生成
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre
Zhongke Panyun - module a infrastructure setting and safety reinforcement scoring standard
[matlab] matlab simulation modulation system FM system
【MATLAB】MATLAB 仿真 — 窄带高斯白噪声
COMP1721 Creating Classes
抓包整理外篇fiddler———— 会话栏与过滤器
STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管
2022广东省赛——编码信息获取 解析flag
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
我们认为消费互联网发展到最后,依然会局限于互联网行业本身
小程序毕业设计---美食、菜谱小程序
Download kicad on Alibaba cloud image station
记几个智能手表相关芯片 蓝牙芯片 低功耗
EVM proof in appliedzkp zkevm (11)
![[QT] timer](/img/df/5db6af851ef19f33fd7e7a7ed46586.png)








