菜鸟专用
简单移动和跳跃(可添加音乐脚步)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaterControl : MonoBehaviour
{
//刚体
private Rigidbody rBody;
//是否在地面上
private bool isGround;
//音频组件
private AudioSource footPlayer;
void Start()
{
//获得刚体
rBody = GetComponent<Rigidbody>();
//获取声音组件
footPlayer = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
//如果按下空格键2.角色在地面上
if(Input .GetKeyDown (KeyCode.Space )&&isGround ==true )
{
//跳跃:给一个向上的力
rBody.AddForce(Vector3.up * 200);
}
//是否按下移动键
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis("Vertical");
//一定按了移动按键并且角色在地面上
if((horizontal !=0 || vertical !=0)&&isGround ==true )
{
//移动了,并且没有播放声音,在播放声音

这篇博客适合Unity3D新手,详细介绍了如何实现角色的简单移动和跳跃功能,同时提及了可选的音乐脚步效果添加。
最低0.47元/天 解锁文章
6111

被折叠的 条评论
为什么被折叠?



