一:角色跳跃(地面检测)
RaycastHit2D hit = Physics2D.Raycast(groundCheckPositon.position, -transform.up, rayLength, groundLayer);
Debug.DrawLine(groundCheckPositon.position, hit.point, Color.red);
if (hit.collider != null)
{
isGround = true;
}
else
{
isGround = false;
}
二:角色水平移动
h = Input.GetAxis("Horizontal"); //水平方向按键偏移量
move = h * moveSpeed; //刚体具体速度
transform.Translate(new Vector3(move, 0, 0)*Time.deltaTime);
角色移动控制完整脚本二
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public Transform groundCheckPosition;//从此处发射一个射线向下检测地面 一般为玩家自身即可
public float rayLength;//检测地面的射线长度 根据角色高度灵活调整
public LayerMask groundLayer;//检测的地面层级
bool isGround;//判断主角当前是否在地面上
public float dropConst;//下坠常数
public float speed;//地面移动速度
public float jumpSpeedUp;//上升速度
public float JumpSpeedVertical;//空中左右移动速度
Rigidbody2D rig;
Animator Anim;
// Start is called before the first frame update
void Start()
{
rig = transform.GetComponent<Rigidbody2D>();
Anim = transform.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
CheckOnGround();
Move();
}
void Move()
{
float h = Input.GetAxis("Horizontal");
//跳跃
if (Input.GetKeyDown(KeyCode.W) && isGround)
{
//rig.AddForce(Vector2.up * JumpForce);
rig.velocity = new Vector2(0, jumpSpeedUp);
}
//长按高跳 长按高跳异常则可将此部分注释掉
if (rig.velocity.y > 0 && Input.GetKey(KeyCode.W) && !isGround)
{
rig.velocity += Vector2.up * 0.2f;
}
//优化手感
float a = dropConst * 5 - Mathf.Abs(rig.velocity.y);//通过下坠常数,空中速度快为0时,下坠常数a越大,即越快速 度过这个状态
rig.velocity -= Vector2.up * a * Time.deltaTime;
//左右移动
Vector3 vt = new Vector3(h, 0, 0).normalized;
if (h != 0)
{
if (h > 0)
{
transform.GetComponent<SpriteRenderer>().flipX = true;
}
else if (h < 0)
{
transform.GetComponent<SpriteRenderer>().flipX = false;
}
Anim.SetBool("IsWalk", true);
transform.Translate(vt * speed * JumpSpeedVertical * Time.deltaTime);
}
else
{
Anim.SetBool("IsWalk", false);
transform.Translate(vt * speed * Time.deltaTime);
}
}
/// <summary>
/// 检测主角是否在地面上
/// </summary>
void CheckOnGround()
{
RaycastHit2D hit = Physics2D.Raycast(groundCheckPosition.position, -transform.up, rayLength, groundLayer);
Debug.DrawLine(groundCheckPosition.position, hit.point, Color.green);
if (hit.collider != null)
{
isGround = true;
}
else
{
isGround = false;
}
}
}
三:2D游戏中相机跟随(此脚本挂载游戏玩家身上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
private Vector3 mTragetPos = Vector3.zero;
void LateUpdate()
{
mTragetPos = GetCameraMovePos();
if (mTragetPos != Camera.main.transform.position)
{
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, mTragetPos, 10f);
}
}
//地图的边界,通过下面两个点确定相机跟随的一个范围
//地图左下角点
public Transform LeftDown;
//地图右上角的点
public Transform RightUp;
Vector3 GetCameraMovePos()
{
Vector3 pos = this.transform.position;
float screenX = SceneToWorldSize(Screen.width * 0.5f, Camera.main,pos.z);
float screenY = SceneToWorldSize(Screen.height * 0.5f, Camera.main, pos.z);
pos.z = Camera.main.transform.position.z;
float maxX = RightUp.position.x;
float minX = LeftDown.position.x;
if (pos.x - screenX < minX)
{
pos.x = minX + screenX;
}
else if (pos.x + screenX > maxX)
{
pos.x = maxX - screenX;
}
float maxY = RightUp.position.y;
float minY = LeftDown.position.y;
if (pos.y - screenY < minY)
{
pos.y = minY + screenY;
}
else if (pos.y + screenY > maxY)
{
pos.y = maxY - screenY;
}
return pos;
}
/// <summary>
/// 像素单位转世界单位
/// </summary>
/// <param name="size"></param>
/// <param name="ca"></param>
/// <returns></returns>
public float SceneToWorldSize(float size, Camera ca, float Worldz)
{
if (ca.orthographic)
{
float height = Screen.height / 2;
float px = (ca.orthographicSize / height);
return px * size;
}
else
{
float halfFOV = (ca.fieldOfView * 0.5f);//摄像机夹角 的一半//
halfFOV *= Mathf.Deg2Rad;//弧度转角度//
float height = Screen.height / 2;
float px = height / Mathf.Tan(halfFOV);//得到应该在的Z轴//
Worldz = Worldz - ca.transform.position.z;
return (Worldz / px) * size;
}
}
}
四:2D游戏简易摇杆
1: 自制简易摇杆
①:如图所示创建三个Image,层级如下所示。
②:将上图所示PlayerCtroller图片的透明度调为0,并添加ScrollRect组件,并进行如下图所示赋值:
PS:摇杆背景和摇杆赋值千万不能出错
③:摇杆背景和摇杆选择自己喜欢的图片,并进行复制即可
在这里提供一下我项目里使用的摇杆背景,需要自取。也可以用系统自带的图片进行相应的调整。
2: 自制简易摇杆控制2D玩家的移动。
①:先创建一个脚本,脚本内容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class M_PlayerMoveCtroller : MonoBehaviour
{
public RectTransform Image_three;//获取摇杆
Vector3 Begin_image; //定义Image three的初始位置向量
float x, y; //定义摇杆拖动后x轴、y轴产生的偏差
Rigidbody2D rig;
Animator anim;
public float Speed;
private void Start()
{
Begin_image = Image_three.position;
rig = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
//求出拖动摇杆后,摇杆与初始时x轴、y轴的差值
x = Image_three.position.x - Begin_image.x;
y = Image_three.position.y - Begin_image.y;
if (x>0)
{
transform.GetComponent<SpriteRenderer>().flipX = true;
}
else
{
transform.GetComponent<SpriteRenderer>().flipX = false;
}
Vector2 position = rig.position;
//如果x轴差值大于1,则让物体在x轴同步运动
if (Mathf.Abs(x) > 1)
{
//transform.Translate(Speed * Time.deltaTime * x / Mathf.Abs(x), 0, 0);
position.x += (x / Mathf.Abs(x)) * Speed * Time.deltaTime;
}
//如果y轴的差值大于1,则让物体在y轴同步运动
if (Mathf.Abs(y) > 1)
{
//transform.Translate(0, Speed * Time.deltaTime * y / Mathf.Abs(y),0);
position.y += (y / Mathf.Abs(y)) * Speed * Time.deltaTime;
}
rig.MovePosition(position);
if (Mathf.Abs(x) > 1|| Mathf.Abs(y) > 1)
{
anim.SetBool("IsWalk", true);
}
else
{
anim.SetBool("IsWalk", false);
}
}
}
②:将此脚本挂载在2D角色上,并将我们创建的摇杆赋值上去,自行调节移动速度。
然后添加刚体组件,如下图所示:
五:2d游戏游戏角色与场景物体层级关系问题
①:设置两个sprite 处于相同的sortinglayout
②:同时打开Edit->Project Setting->Graphics 设置如下
设置sort mode 为custom Axis 更改为(x:0,y:1,z:0)
效果如下: