您的位置:首页 > 移动开发 > Unity3D

解决 unity 2d 中人物碰撞后抖动旋转问题

2020-07-29 13:12 1201 查看

碰撞后抖动问题的解决:

因为人物添加了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 update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void 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轴即可:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: