【问题标题】:Unity: When my Player touches another object it gets a wierd constant pushback motionUnity:当我的玩家触摸另一个对象时,它会得到一个奇怪的持续推回动作
【发布时间】:2021-11-06 01:08:04
【问题描述】:

我有一个奇怪的问题,似乎无法解决它

我正在统一创建我的第一个游戏,我在创建运动系统后进行了一些测试,每当我触摸另一个对象(无论它是否具有刚体)时,我的玩家都会突然开始自己移动。

我有一个视频显示了到底发生了什么:https://youtu.be/WGrJ0KNYSr4

我已经尝试了一些不同的事情,并且我确定它必须与物理引擎做一些事情,因为它只在玩家不是运动学时发生,所以我尝试在项目设置中增加物理求解器迭代,但是错误仍在发生,所以我在互联网上寻找答案,但我发现的唯一一件事是删除 Time.deltaTime ,但它仍然没有工作。 我发现当玩家快速移动时,这种情况似乎较少发生。

如果有人能帮我解决这个问题,我将不胜感激,因为这是我的第一个真正的游戏,并且我正在为 itch.io 上发生的 Seajam 创建它。

这是我的 playercontroller 脚本的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float playerSpeed = 3f;
private Rigidbody playerRb;
public GameObject cameraRotation;
// Start is called before the first frame update
void Start()
{
    playerRb = GetComponent<Rigidbody>();
}
float verticalSpeed;
float horizontalSpeed;
bool isOnGround = true;

// Update is called once per frame
void Update()
{
    //get controlled forward and backward motion
    verticalSpeed = Input.GetAxis("Vertical");
    transform.Translate(Vector3.forward * playerSpeed * verticalSpeed * Time.deltaTime);

    //get controlled sidewards motion
    horizontalSpeed = Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * playerSpeed * horizontalSpeed * Time.deltaTime);

    //lock the rotation of the player on the z and x axis
    transform.eulerAngles = new Vector3(0, cameraRotation.transform.eulerAngles.y, 0);

    //when pressing space jump and prevent air jump
    if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
    {
        playerRb.AddForce(Vector3.up * 10, ForceMode.Impulse);
        isOnGround = false;
    }
}
//check if the player is on the ground
private void OnCollisionEnter(Collision collision)
{
    isOnGround = true;
}

}

【问题讨论】:

标签: c# unity3d


【解决方案1】:

尝试不要通过脚本锁定播放器的旋转,可能是由于云台锁定导致的问题。而是转到玩家的 RigidBody->Constraints,然后将其锁定。 你可以在这里阅读更多关于它的信息https://fr.wikipedia.org/wiki/Blocage_de_cardan

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多