using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerCon : MonoBehaviour
{
public float smoothing = 10;
private Vector2 targetPos = new Vector2(1f, 1f);
private int posx = 1;
private int posy = 1;
private Rigidbody2D rig;
private Collider2D collider;
private float restTime = 0.5f;
private float restTimer = 1f;//定义一个计时器 多久可以进行一次移动的输入
private void Awake()
{
restTimer = restTime;//
rig = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>();
}
//这种方式用于2D控制角色移动的效果 是角色可以斜着走
private void Update()
{
rig.MovePosition(Vector2.Lerp(transform.position, targetPos, smoothing * Time.deltaTime));//这里移动有点先快后慢
restTimer -= Time.deltaTime;
if (restTimer >= 0)
{
return;
}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (h != 0)//这一个是水平优先 确保不能斜着走,但是因为移动用了rig.MovePosition() 括号里面用了插值移动,所以还是可以稍微斜着走
{
v = 0;
}
if (h != 0 || v != 0)
{
collider.enabled = false;
RaycastHit2D hit = Physics2D.Linecast(targetPos, targetPos + new Vector2(h, v));//这里判断是否可以移动到下个位置,在目标点和下一个目标点之间发射一条射线
collider.enabled = true;
if (hit.transform == null)
{
targetPos += new Vector2(h, v);
restTimer = restTime;
}
else
{
switch (hit.collider.tag)//如果射到了就进行想要的操作
{
case "OutWall":
break;
case "Wall":
break;
default:
break;
}
}
restTimer = restTime;
}
}
}
以上这个方法缺点是角色可能会稍微斜着移动 ,因为rig.moveposition 用了插值lerp,插值也是这个
改进 去掉插值和输入控制计时器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playCon2 : MonoBehaviour {
public float smoothing = 10;
private Vector2 targetPos = new Vector2(1f, 1f);
private int posx = 1;
private int posy = 1;
private Rigidbody2D rig;
private Collider2D collider;
//private float restTime = 0.5f;
//private float restTimer = 1f;//定义一个计时器 多久可以进行一次移动的输入
private void Awake()
{
// restTimer = restTime;
rig = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>();
}
//这种方式用于2D控制角色移动的效果 是角色可以斜着走
private void Update()
{
rig.MovePosition( targetPos);
//restTimer -= Time.deltaTime;
//if (restTimer >= 0)
//{
// return;
//}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (h != 0)//这一个是水平优先 确保不能斜着走,但是因为移动用了rig.MovePosition() 括号里面用了插值移动,所以还是可以稍微斜着走
{
v = 0;
}
if (h != 0 || v != 0)
{
collider.enabled = false;
RaycastHit2D hit = Physics2D.Linecast(targetPos, targetPos + new Vector2(h, v));//这里判断是否可以移动到下个位置,在目标点和下一个目标点之间发射一条射线
collider.enabled = true;
if (hit.transform == null)
{
targetPos += new Vector2(h, v);
// restTimer = restTime;
}
else
{
switch (hit.collider.tag)//如果射到了就进行想要的操作
{
case "OutWall":
break;
case "Wall":
break;
default:
break;
}
}
//restTimer = restTime;
}
}
}
改进之后角色可以上下左右移动 但是就是没有插值移动
移动也可以使用transform.translate 这种写法,下面就是利用transform.translate 进行移动,然后利用射线判断是否可以继续往下走,而不是用unity自带的刚体碰撞进行阻挡判断
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playCon2 : MonoBehaviour {
public float smoothing = 10;
private Vector2 targetPos = new Vector2(1f, 1f);
private int posx = 1;
private int posy = 1;
private Rigidbody2D rig;
private Collider2D collider;
private void Awake()
{
rig = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>();
}
private void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (h != 0)//这一个是水平优先 确保不能斜着走
{
v = 0;
}
if (h != 0 || v != 0)
{
collider.enabled = false;
RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector3(h, v, 0),0.5f);//这里判断是否可以移动到下个位置,在目标点和下一个目标点之间发射一条射线
Debug.DrawRay(transform.position, new Vector3(h, v, 0).normalized * 0.1f, Color.red, 2f);
// RaycastHit2D hit = Physics2D.Linecast(transform.position, transform.position + new Vector3(h, v ,0));//这里判断是否可以移动到下个位置,在目标点和下一个目标点之间发射一条射线
collider.enabled = true;
if (hit.transform == null)
{
transform.Translate(Vector3.right * h * Time.deltaTime + Vector3.up * v * Time.deltaTime);//没有碰撞到物体就继续移动
}
else
{
switch (hit.collider.tag)//如果射到了就进行想要的操作
{
case "OutWall":
break;
case "Wall":
break;
default:
break;
}
}
}
}
}
最后一种就是简单的利用unity 刚体+碰撞体进行移动控制 和 transform.Translate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercon3 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
transform.Translate(transform.right * h*Time.deltaTime);
//中间写其他想要的功能代码
if (h != 0)//水平移动优先
{
return;
}
float v = Input.GetAxis("Vertical");
transform.Translate(transform.up * v * Time.deltaTime);
}
}