一、TileMap的管理
在创建地图的时候,为了方便管理和调用,可以创建不同的Palette,这些不同的Palette可以挂载不同的组件来达到不一样的效果。
Rule Tile:
一键生成有规则的tile map
创建后贴上专门的贴图并在九宫格上设计规则,上层的规则优先执行,创建之后,将其拖拽至Tile Palette中就可以使用。
如果要随机生成也可以设置
碰撞器的添加
可以将场景中的平台的碰撞器符合为一个整体
将其勾选,然后再添加Composite Collide 2D的组件就可
二、player的Rigidbody 2D
在使用RigidBody的时候可以将collision detection改为Continuous,可以让其的碰撞方式改为连续不断的碰撞,使运行更加流畅。
在Interpoate中选择Interpoate差值选项,可以让角色从地面掉落使微微凹陷再弹起,从而优化手感。
三、移动代码的优化
1)Header的使用
[Header("移动")]
public float speed = 8f;
public float crouchSpeedDivisor = 3f;
public float xVelocity;
[Header("跳跃参数")]
public float jumpForce = 6.3f;
public float jumpHoldForce = 1.9f;
public float jumpHoldDuration = 0.1f;
public float crouchJumpBoost = 2.5f;
public float jumpTime;
public float hangingJumpFroce = 15f;
使用后可以再unity中展现效果
2)按键的优化
getKey和getKeyDown的区别为getkey为持续判断也就是下压的过程,而keydown是判断一次是否下按。
为了弥补在操作过程中按键不灵敏,可以再update的函数中调用,将按键状态赋予bool数,从而优化实际操作
void Update()
{
if(!jumpPressed)
{
jumpPressed = Input.GetButtonDown("Jump");
}
jumpHeld = Input.GetButton("Jump");
crouchHeld = Input.GetButton("Crouch");
crouchPressed = Input.GetButtonDown("Crouch");
}
3)射线的使用
通过在脚本中添加射线,可以更准确的判断任务的状态,从而更精准的调用角色。
对射线方法的重新调用
RaycastHit2D Raycast(Vector2 offset,Vector2 rayDiraction,float length,LayerMask layer)
{
Vector2 pos=transform.position;
RaycastHit2D hit = Physics2D.Raycast(pos + offset, rayDiraction, length, layer);
Color color=hit?Color.red:Color.green;
Debug.DrawRay(pos+offset,rayDiraction,color);
return hit;
}
其真实效果为bool值
//射线的使用
RaycastHit2D leftCheck=Raycast(new Vector2(-footOffset,0f),Vector2.down,groundDistance,groundLayer);
RaycastHit2D rightCheck = Raycast(new Vector2(footOffset, 0f), Vector2.down, groundDistance, groundLayer);
RaycastHit2D headerCheck = Raycast(new Vector2(0f, coll.size.y), Vector2.up, headClearance, groundLayer);
四、动画管理
整型的调用,字符型的传递可能会出现问题(在移动端)
[Header("动画编号")]
private int groundID;
private int hangingID;
private int crouchID;
private int speedID;
private int fallID;
void Start()
{
anim = GetComponent();
rb = GetComponentInParent();
moveMent = GetComponentInParent();
groundID = Animator.StringToHash("isOnGround");
hangingID = Animator.StringToHash("isHangjing");
crouchID = Animator.StringToHash("isCrouching");
speedID = Animator.StringToHash("speed");
fallID = Animator.StringToHash("verticalVelocity");
}
// Update is called once per frame
void Update()
{
anim.SetFloat(speedID, Mathf.Abs(moveMent.xVelocity));
anim.SetBool(groundID,moveMent.isOnGround);
anim.SetBool(hangingID, moveMent.isHanging);
anim.SetBool(crouchID, moveMent.isCrouch);
anim.SetFloat(fallID, rb.velocity.y);
}
blend tree
在此改变并按数值大小排序
五、声音管理
通过在脚本中的调用来生成声音
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class AudioManger : MonoBehaviour
{
static AudioManger current;
[Header("环境声音")]
public AudioClip ambientClip;
public AudioClip musicClip;
[Header("Robbie音效")]
public AudioClip[] walkStepClips;
public AudioClip[] crouchStepClips;
public AudioClip jumpClip;
public AudioClip jumpVoiceClip;
AudioSource ambientSource;
AudioSource musicSource;
AudioSource fxSource;
AudioSource playerSource;
AudioSource voiceSource;
private void Awake()
{
current = this;
DontDestroyOnLoad(gameObject);
ambientSource=gameObject.AddComponent();
musicSource=gameObject.AddComponent();
fxSource=gameObject.AddComponent();
playerSource=gameObject.AddComponent();
voiceSource=gameObject.AddComponent();
StartLevelAudio();
}
private void StartLevelAudio()
{
current.ambientSource.clip = current.ambientClip;
current.ambientSource.loop = true;
current.ambientSource.Play();
current.musicSource.clip = current.musicClip;
current.musicSource.loop = true;
current.musicSource.Play();
}
public static void PlayFootstepAudio()
{
int index=Random.Range(0,current.walkStepClips.Length);
current.playerSource.clip=current.walkStepClips[index];
current.playerSource.Play();
}
public static void PlayCrouchAudio()
{
int index = Random.Range(0, current.crouchStepClips.Length);
current.playerSource.clip = current.crouchStepClips[index];
current.playerSource.Play();
}
public static void PlayJumpAudio()
{
current.playerSource.clip = current.jumpClip;
current.playerSource.Play();
current.voiceSource.clip = current.jumpVoiceClip;
current.voiceSource.Play();
}
}