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);

}
}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 开摩托车忘记带安全头盔怎么办 gta不想要车了怎么办 空气滤芯进水会怎么办 车胎扎了个钉子怎么办 德罗索没导弹了怎么办 CF手雷包不能用怎么办 cfAK爆头碰到狙怎么办 玩cf网络延迟高怎么办 逆水寒装备分解错了怎么办 轴与孔间隙过大怎么办 小孩眼睛被打了怎么办 玩王者荣耀手机屏幕竖着怎么办 棉被被老鼠尿湿怎么办 打完子弹能下来怎么办 怪物猎人 弩子弹打完了怎么办 烤瓷牙龈发黑了怎么办 做彩超前喝水了怎么办 胸贴过敏红痒怎么办 芡粉里面有虫了怎么办 勾芡淀粉放多了怎么办 剁排骨没有好刀怎么办 硬币掉进档位里怎么办 魅族mx5手机太卡怎么办 华为m9手机声音小怎么办 放卡的地方堵住怎么办 美图m8蓝屏了怎么办 美图m8手机蓝屏怎么办 美图m6s手机白屏怎么办 魅蓝2开不开机怎么办 魅族手机主键没反应怎么办 魅族手机主键失灵怎么办 手机4g网络不稳定怎么办 华为m9收不到手机信息怎么办 华为手机wifi信号弱怎么办 手机连接wifi信号差怎么办 华华为p10信号不好怎么办 烟没拆封受潮了怎么办 和亲儿子发生了性关系怎么办 无心磨磨出来圆度不好怎么办 中国人在越南办结婚证怎么办? 无线网被限速了怎么办