unity 2d 如何停止所有朝向对撞机的运动以防止实体摇晃
unity 2d how to stop all movement towards a collider to prevent an entity from shaking
问题很简单:我有一个带有 2d 刚体和 2d 对撞机的播放器,我有一个带有 2d 对撞机的墙。当我将播放器靠在墙上时,它们确实会发生碰撞,但我的播放器开始快速摇晃,看起来它想要穿过墙壁。
我需要玩家在撞到墙时停止移动,并在不中断远离墙的情况下拒绝向墙进一步移动。
玩家移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// Variables
public bool moving = true;
float playerMovSpeed = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (moving == true)
{
PlayerMove();
}
PlayerMoveCheck();
}
public void Set_moving(bool val)
{
moving = val;
}
void PlayerMove()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.up * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.down * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * playerMovSpeed * Time.deltaTime, Space.World);
}
}
void PlayerMoveCheck()
{
if (Input.GetKey(KeyCode.W) != true && Input.GetKey(KeyCode.A) != true && Input.GetKey(KeyCode.S) != true && Input.GetKey(KeyCode.D) != true)
{
moving = false;
}
else
{
moving = true;
}
}
提前致谢。
transform.Translate() 本质上充当了变换的传送。这意味着你的玩家被放入新坐标,然后看到有碰撞并将其推出 - 这就是为什么你的角色 "vibrates" 靠在墙上。相反,您应该考虑向您的播放器添加 rigidbody 并使用 .velocity
或 .position
或“.AddForce()”修改运动,这将在运动。
你可以像下面这样进行强制移动:
void Update()
{
rbody.velocity = rbody.velocity * .9f; // custom friction
if (Input.GetKey(KeyCode.W))
rbody.AddForce(Vector2.up * speed);
if (Input.GetKey(KeyCode.S))
rbody.AddForce(Vector2.down * speed);
// repeat for A and D
}
问题很简单:我有一个带有 2d 刚体和 2d 对撞机的播放器,我有一个带有 2d 对撞机的墙。当我将播放器靠在墙上时,它们确实会发生碰撞,但我的播放器开始快速摇晃,看起来它想要穿过墙壁。
我需要玩家在撞到墙时停止移动,并在不中断远离墙的情况下拒绝向墙进一步移动。
玩家移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// Variables
public bool moving = true;
float playerMovSpeed = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (moving == true)
{
PlayerMove();
}
PlayerMoveCheck();
}
public void Set_moving(bool val)
{
moving = val;
}
void PlayerMove()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.up * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.down * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * playerMovSpeed * Time.deltaTime, Space.World);
}
}
void PlayerMoveCheck()
{
if (Input.GetKey(KeyCode.W) != true && Input.GetKey(KeyCode.A) != true && Input.GetKey(KeyCode.S) != true && Input.GetKey(KeyCode.D) != true)
{
moving = false;
}
else
{
moving = true;
}
}
提前致谢。
transform.Translate() 本质上充当了变换的传送。这意味着你的玩家被放入新坐标,然后看到有碰撞并将其推出 - 这就是为什么你的角色 "vibrates" 靠在墙上。相反,您应该考虑向您的播放器添加 rigidbody 并使用 .velocity
或 .position
或“.AddForce()”修改运动,这将在运动。
你可以像下面这样进行强制移动:
void Update()
{
rbody.velocity = rbody.velocity * .9f; // custom friction
if (Input.GetKey(KeyCode.W))
rbody.AddForce(Vector2.up * speed);
if (Input.GetKey(KeyCode.S))
rbody.AddForce(Vector2.down * speed);
// repeat for A and D
}