碰撞后抖动问题的解决:
因为人物添加了Box Collider 2D 和刚体,因此当碰撞后会模拟实际的运动情况,和其它碰撞体碰撞后会发生抖动;
解决方法:
通过刚体控制物体的运动和位置,而不是通过 transfrom.position 来获得物体的位置并更新;
using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerController : MonoBehaviour{public float speed = 5f;Rigidbody2D rbody;//刚体组件// Start is called before the first frame updatevoid Start(){rbody = GetComponent<Rigidbody2D>();}// Update is called once per framevoid Update(){float moveX = Input.GetAxisRaw("Horizontal");float moveY = Input.GetAxisRaw("Vertical");Vector2 position = rbody.position;position.x += moveX * speed * Time.deltaTime;position.y += moveY * speed * Time.deltaTime;rbody.MovePosition(position);}}
解决碰撞后角色会旋转的问题
通过为Rigidbody 2D 添加约束实现,冻结z轴即可: