【问题标题】:How would I increase the friction of a car so that it doesn't glide when I don't give it any inputs?当我不给它任何输入时,我将如何增加汽车的摩擦力使其不会滑行?
【发布时间】:2020-08-07 22:37:54
【问题描述】:

每个人。我是 Unity 2D 和 C# 的新手,我想知道如何阻止我的车在我的游戏中滑行和向前滑动,因为如果我不希望它这样做,因为如果它滑得太厉害了。我希望这样,如果我不给汽车任何输入,或者给汽车几乎立即停止或开始向另一方向移动的计数器输入。我尝试过创建 Physics2D 材质并增加摩擦力,但它确实对游戏玩法没有影响。我也尝试过检查,如果没有玩家输入,则 rb.velocity 设置为 0,或当前值的 0.2 左右,因此它是一个更平滑的停止,但只有在玩家移动然后放开移动键,如果他们尝试向相反的方向移动,则不会。我还考虑过通过将先前位置与当前位置进行比较来检查汽车的移动方向,然后向相反方向增加力以停止汽车,但这似乎非常耗费性能,我真的不知道我会去哪里开始。如果有人对我将如何做这件事有任何建议,请告诉我如何做。这是我到目前为止的代码:

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

public class CarController : MonoBehaviour
{

public Rigidbody2D carRigidbody;
public Rigidbody2D backTire;
public Rigidbody2D frontTire;

private float movement;
public float fspeed = 100;
public float bspeed = 60;
public float carTorque = 10;
public float decVel = .9f;
private bool beganMoving;



// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    movement = Input.GetAxis("Horizontal");
}

void FixedUpdate() {
    if (movement == 1 || movement == -1) {
        beganMoving = true;
    }
    if (movement == 0 && beganMoving) {
        backTire.velocity = new Vector2 (backTire.velocity.x * decVel * Time.fixedDeltaTime, backTire.velocity.y); 
        frontTire.velocity = new Vector2 (backTire.velocity.x * decVel * Time.fixedDeltaTime, backTire.velocity.y);
        
    }
    
    
    backTire.AddTorque(-movement * bspeed * Time.fixedDeltaTime);
    frontTire.AddTorque(-movement * fspeed * Time.fixedDeltaTime);
    carRigidbody.AddTorque(-movement * carTorque * Time.fixedDeltaTime);
}

}

【问题讨论】:

    标签: c# unity3d game-physics


    【解决方案1】:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CarController : MonoBehaviour
    {
    
    public Rigidbody2D carRigidbody;
    public Rigidbody2D backTire;
    public Rigidbody2D frontTire;
    
    private float movement;
    public float fspeed = 100;
    public float bspeed = 60;
    public float carTorque = 10;
    public float decVel = .9f;
    private bool beganMoving;
    
    
    // Update is called once per frame
    void Update()
    {
        movement = Input.GetAxis("Horizontal");
    }
    
    void FixedUpdate() {
        if (movement == 1 || movement == -1) {
            beganMoving = true;
        }
        if (movement <= 0.1f)
        {
            beganMoving = false;
        }
        if (movement <= 0.8f && beganMoving) // If the movement is starting to decrease and it the car has already started moving, set the velocities to zero.
        {
            backTire.velocity = Vector3.zero;
            frontTire.velocity = Vector3.zero;
        }
        if (movement == 0 && beganMoving) {
            backTire.velocity = new Vector2 (backTire.velocity.x * decVel * Time.fixedDeltaTime, backTire.velocity.y); 
            frontTire.velocity = new Vector2 (backTire.velocity.x * decVel * Time.fixedDeltaTime, backTire.velocity.y);
            
        }
        
        
        backTire.AddTorque(-movement * bspeed * Time.fixedDeltaTime);
        frontTire.AddTorque(-movement * fspeed * Time.fixedDeltaTime);
        carRigidbody.AddTorque(-movement * carTorque * Time.fixedDeltaTime);
    }
    

    【讨论】:

    • 我收到“CircleCollider2D”不包含“材料”定义的错误,并且当前上下文中不存在 statFriction 和 dynFriction?我需要为 stat 和 dynFriction 定义一些变量吗?我需要写physicsMaterial2D而不是material吗?
    • @HectorAstrom 抱歉,我现在正在编辑它。 (我给出的答案好像 coll 是 Collider,而不是 CircleCollider2D
    • 是的,我基本上只是假设,对撞机意味着我的轮子的 cirlce collider 2d,但这是一个不正确的假设,我应该使用通用对撞机吗?圆形对撞机 2d 是我遇到上述错误的原因。
    • 我看到您编辑了您的原始帖子。这是我正在考虑设置物理材料的方式,但增加车轮上的摩擦似乎没有任何作用。我知道材料应用正确,就好像我改变了它在游戏中更新的弹性一样,但如果我改变了摩擦力,或者至少它不明显。
    • @HectorAstrom 我认为这是因为您的代码与对撞机的设置没有任何关系。
    【解决方案2】:

    大家好,浏览过这个问题的人都有类似的问题。我最终解决这个问题的方法是在这里实现整个代码块:

    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    
        if (movement == 1 || movement == -1) {
            beganMoving = true;
        } // this makes sure we've started moving which will be used later
        if (movement == 0 && isGrounded) {
            backTire.velocity = new Vector2 (backTire.velocity.x * .8f * Time.fixedDeltaTime, backTire.velocity.y); 
            frontTire.velocity = new Vector2 (frontTire.velocity.x * .8f * Time.fixedDeltaTime, frontTire.velocity.y);  // this whole if statement is so that our car will stop or decelerate when no input is given to it as to stop  it from rolling all across the ground
        }
        if (movement > 0 && prevMove < 0) { // checks if we are moving forward (right) when we used to move backwards (left)
            backTire.velocity = new Vector2 (backTire.velocity.x * decVel * Time.fixedDeltaTime, backTire.velocity.y); 
            frontTire.velocity = new Vector2 (frontTire.velocity.x * decVel * Time.fixedDeltaTime, frontTire.velocity.y); //stops us if the if statement returns true
            prevMove = 1; // sets our previous movement to moving forward to make it not repeat itself and to check with later on
        } else if (movement < 0 && prevMove > 0 ) { // checks if we are moving backwards when we used to be moving forwards
            backTire.velocity = new Vector2 (backTire.velocity.x * decVel * Time.fixedDeltaTime, backTire.velocity.y); 
            frontTire.velocity = new Vector2 (frontTire.velocity.x * decVel * Time.fixedDeltaTime, frontTire.velocity.y); //stops car
            prevMove = -1; // sets previous movement to moving backwards
        } else {
            if (beganMoving == true) { // when we start moving we set the previous movement to which ever direction we decided to first move in
            prevMove = movement;
            beganMoving = false; // set began moving to false. This doesn't make sense logically since we did begin moving but it's so this if statement doesn't repeat itself again
            }
        }
    

    我已将 cmets 包括在内,因此您有望了解我做了什么以及为什么这样做。我希望这可以帮助任何有类似问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多