Unity 2D角色控制脚本C#直接上代码
来源:互联网 发布:linux sftp 端口 编辑:程序博客网 时间:2024/06/06 07:35
using UnityEngine;
public class Controller : MonoBehaviour
{
public float moveForce = 365f;
public float maxSpeed = 4f;
private Animator animator;
private SpriteRenderer spriteRender;
//private float timelost = 0;
[HideInInspector]
public bool facingRight = true;
[HideInInspector]
public AudioClip[] jumpClips; // 当玩家跳跃时剪辑的数组。
[HideInInspector]
public float jumpForce = 1000f; // 球员跳跃时增加的力量。
[HideInInspector]
public bool jump = false;
//bool KEY_A = Input.GetKey(KeyCode.A);
//bool KEY_D = Input.GetKey(KeyCode.D);
//bool KEY_S = Input.GetKey(KeyCode.S);
//bool KEY_W = Input.GetKey(KeyCode.W);
//bool KEY_SPACE = Input.GetKey(KeyCode.Space);
private Transform groundCheck;
//检查是否接地
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded = false; //球员是否被接地。
void Awake()
{
groundCheck = transform.GetChild(0);
animator = GetComponent<Animator>();
}
void Start()
{
}
void Update()
{
}
void FixedUpdate()
{
animator = this.GetComponent<Animator>();
spriteRender = gameObject.GetComponent<SpriteRenderer>();
// 如果在地面层。
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
// 如果跳跃按钮被按下,落地,可以跳。
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
jump = true;
}
//if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
//{
//}
// 缓存水平输入。
float h = Input.GetAxis("Horizontal");
// The Speed animator parameter is set to the absolute value of the horizontal input.
// animator.SetFloat("Speed", Mathf.Abs(h));
// 如果球员改变方向(H具有不同签名速度。x)或没有达到最大
if (h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed)
{
GetComponent<Rigidbody2D>().AddForce(Vector2.right * h * moveForce);
//transform.Translate(new Vector3(h, 0, 0) * maxSpeed * Time.deltaTime);
// GetComponent<Rigidbody2D>().AddForce(Vector2.right * h * moveForce);
}
if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
{
//…设置玩家的速度在X轴电动机。
//animator.SetBool("IsRun", false);
//transform.Translate(new Vector3(h, 0, 0) * maxSpeed * Time.deltaTime);
animator.SetBool("IsRun", false);
GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
// 面临左
if (h > 0 && !facingRight)
{
Flip();
}
// 面朝右…
else if (h < 0 && facingRight)
{
Flip();
}
if (jump)
{
// 设置跳转动画触发参数。
// animator.SetBool("IsJump",false);
// 播放随机跳转音频剪辑。
int i = Random.Range(0, jumpClips.Length);
// AudioSource.PlayClipAtPoint(jumpClips[i], transform.position);
// 向玩家添加垂直力。
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));
//确保玩家在更新的跳转条件满足之前不能再次跳转。
jump = false;
}
}
// 切换玩家被标记为“面朝”的方式。
void Flip()
{
facingRight = !facingRight;
//将玩家的x区域比例乘以1。
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void LateUpdate()
{
animator.SetBool("IsRun", true);
}
}
阅读全文
0 0
- Unity 2D角色控制脚本C#直接上代码
- Unity Player角色移动控制脚本实现
- 【Unity&角色移动】代码控制重力
- Unity用GUI在角色头上显示名字(C#脚本)
- Unity 2D游戏相机控制(水平,竖直,角色居中三种模式)
- UNITY 2D学习笔记(二):C#脚本编写
- Unity使用代码控制2d游戏物体(一)
- unity 3D登录界面C#脚本
- Unity 3d C#脚本(1)
- Unity 3D Coroutine&yield(C#脚本)
- unity用方向键来控制角色上楼梯
- 鼠标控制移动2d角色
- Unity 3D 控制物体上下左右均匀移动脚本
- Unity 3D 对手机屏幕触摸的控制脚本
- Unity 3D 控制物体前后左右均匀移动脚本
- C#脚步控制角色在屏幕上旋转
- Unity 角色控制相关操作
- Unity 角色控制相关操作
- 阅文集团重现盛大往事,多家子公司CEO离职!
- 滴滴又获73亿美元融资 成为全球第三大独角兽
- 比特币开挂疯涨 炮灰明显不够用了
- IEEE院士Ming C. Lin:VR中音频渲染模拟为何这么难?
- 692. Top K Frequent Words
- Unity 2D角色控制脚本C#直接上代码
- 正则表达式【语法、说明、表达式实例】
- 【OpenGL】窗口,视口,裁剪区
- 网站使用地图接口(以百度为例)和转换GPS的使用方法。
- 2017-10-28
- 思维模拟
- Java输出一个数的所有因子并但返回个数的算法
- 对于模板的小总结
- 最小树形图(朱-刘算法)