簡體   English   中英

Unity3D 玩家在石頭上穿行

[英]Unity3D Player walking through and on the Stone

大家好,我的玩家正在石頭上行走並穿過石頭。 名為 Champ 的玩家有一個 Box Collider,而 Stone 有一個 Mesh Collider。 玩家也有剛體。 我嘗試了我發現的一切,但沒有任何幫助解決我的問題。

MovePlayer.cs 腳本

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

public class MovePlayer : MonoBehaviour
{

    Rigidbody rb;

    public float speed = 10f;
    private Vector3 moveDirection;
    public float rotationSpeed = 0.05f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")).normalized;
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + transform.TransformDirection(moveDirection * speed * Time.deltaTime));
        RotatePlayer();
    }

    void RotatePlayer()
    {
        if (moveDirection != Vector3.zero)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection.normalized), rotationSpeed);
        }
        transform.Translate(moveDirection * speed * Time.deltaTime, Space.World);
    }

}

檢查器中的播放器設置

檢查器中的石頭設置

場景預覽

謝謝你們的幫助::)

測試您的代碼和沖突似乎對我來說工作正常。

通過將腳本添加到帶有盒子碰撞器的游戲對象並使用立方體創建一個小關卡來對其進行測試。 還做了一堵牆,我修改為使用網格對撞機而不是盒子對撞機。 玩家與場景中的物體正常碰撞。

您應該從 Project Settings > Physics中仔細檢查您的 Layer 碰撞矩陣,您是否已將圖層播放器和牆設置為碰撞。

  1. 您還可以嘗試將新立方體添加到場景中並將其圖層設置為牆壁以查看玩家是否與它發生碰撞。 如果是這樣,那么石頭的網格可能存在問題。
  2. 如果沒有,那么我會禁用玩家的動畫師和重力體組件,以確保它們不會干擾碰撞

Rigidbody.MovePosition基本上使玩家瞬移,這可能會導致意外行為。 通常建議使用Rigidbody.AddForce代替。 可以使用ForceMode.VeloictyChange進行精確運動。

public float maxVelocityChange = 5.0f;

void moveUsingForces(){

    Vector3 targetVelocity = moveDirection;
    // Doesn't work with the given RotatePlayer implementation.
    // targetVelocity = transform.TransformDirection(targetVelocity);
    targetVelocity *= speed;

    // Apply a force that attempts to reach our target velocity
    Vector3 velocity = rb.velocity;
    Vector3 velocityChange = (targetVelocity - velocity);
    velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    velocityChange.y = 0;
    rb.AddForce(velocityChange, ForceMode.VelocityChange);
}

所以伙計們,我在上面發布的人的幫助下找到了解決方案。

問題是我的玩家速度在代碼中太高了,速度在浮動 10 上,但我在 Unity Inspector of Player 中將速度更改為浮動 50。

所以我解決問題的第一步是將速度設置為浮動10,但我仍然想以50f的速度移動......

此問題的解決方案是在 Unity 2020.3.24f1 及更高版本(可能更低)中,您可以 go 到Edit>Project Settings>Physics並將“ Default Max Depenetration Velocity ”設置為您希望對象停止而不通過的速度。 在我的情況下,我想以speed = 50f移動,所以我需要將Default Max Depenetration Velocity更改為50

我希望我將來可以幫助某人回答這個問題!

最好的祝願 Max G。

搜索幾天后,編輯>項目設置>物理並設置“默認最大穿透速度”解決方案對我有用。 rb.AddForce 不斷穿過一堵也移動的牆。 更改該設置解決了它。

所以非常感謝Max G,愛你!

在此代碼中,您應用了兩次運動,問題是使用了transform.Translate 請記住,剛體Rigidbody方法對碰撞體敏感並能識別它們,但變換不一樣,僅應用點對點移位。 為了解決這個問題,我認為您不需要在旋轉部分使用帶有translate的重復運動代碼。

void RotatePlayer()
{
    if (moveDirection != Vector3.zero)
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection.normalized), rotationSpeed);
    }
    // removed translate
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM