C# 我该如何增加汽车的摩擦力,使其不会';我不滑翔时不要滑翔';你不给它任何信息吗?

C# 我该如何增加汽车的摩擦力,使其不会';我不滑翔时不要滑翔';你不给它任何信息吗?,c#,unity3d,game-physics,C#,Unity3d,Game Physics,各位。我是Unity 2D和C#的新手,我想知道,如果我的车滑得太厉害,我真的感觉不到我能控制住它,我怎么能在我不想让它滑的时候阻止它在我的比赛中不停地向前滑。我希望这样,如果我不给汽车任何输入,或给计数器输入,汽车几乎立即停止或开始向另一个方向移动。我曾尝试过创建Physics2D材质并增加摩擦力,但实际上对游戏没有任何影响。我也尝试过检查,如果没有玩家输入,rb.velocity设置为0,或者大约为当前的0.2,因此它是一个更平滑的停止,但只有当玩家移动然后放开移动键时才有效,而不是当他们尝

各位。我是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);
}
}

使用System.Collections;
使用System.Collections.Generic;
使用UnityEngine;
公共类CarController:单行为
{
公共刚体;
公共刚体2D后轮胎;
公共刚体2D前轮胎;
私人浮动运动;
公共浮点数fspeed=100;
公共浮点数=60;
公共浮动卡通克=10;
公共浮动下降=0.9f;
私人住宅开始搬家;
//每帧调用一次更新
无效更新()
{
移动=输入。GetAxis(“水平”);
}
void FixedUpdate(){
如果(移动==1 | |移动==-1){
beganMoving=true;
}

if(movementHi每个人都浏览过类似的问题。我最终解决这个问题的方法是在这里实现这整段代码:

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
        }
    }
isground=Physics2D.overlapple圆(groundCheck.position,checkRadius,whatIsGround);
如果(移动==1 | |移动==-1){
beganMoving=true;
}//这确保我们已开始移动,稍后将使用
如果(移动==0&&isGrounded){
backTire.velocity=新矢量2(backTire.velocity.x*.8f*时间固定时间,backTire.velocity.y);
frontTire.velocity=new Vector2(frontTire.velocity.x*.8f*Time.fixeddeltime,frontTire.velocity.y);//这个完整的if语句是这样的:当没有输入时,我们的汽车将停止或减速,以阻止它在地面上滚动
}
if(movement>0&&prevMove<0){//检查我们过去向后移动(左)时是否向前移动(右)
backTire.velocity=新矢量2(backTire.velocity.x*devel*Time.fixeddedtime,backTire.velocity.y);
frontTire.velocity=newvector2(frontTire.velocity.x*devel*Time.fixeddeltime,frontTire.velocity.y);//如果if语句返回true,则停止我们
prevMove=1;//将上一个移动设置为向前移动,使其不会重复,并在稍后进行检查
}else if(movement<0&&prevMove>0){//检查我们过去向前移动时是否向后移动
backTire.velocity=新矢量2(backTire.velocity.x*devel*Time.fixeddedtime,backTire.velocity.y);
frontTire.velocity=new Vector2(frontTire.velocity.x*devel*Time.fixedDeltaTime,frontTire.velocity.y);//停车
prevMove=-1;//将上一个移动设置为向后移动
}否则{
如果(beganMoving==true){//当我们开始移动时,我们设置了我们决定首先移动的上一个移动方向
移动=移动;
beganMoving=false;//set开始移动到false。这在逻辑上没有意义,因为我们确实开始移动了,但如果语句不再重复,它就是这样
}
}

我加入了一些评论,希望您能够理解我做了什么以及为什么这么做。我希望这对任何有类似问题的人都有帮助。

我遇到了一些错误,即“CircleCollider2D”不包含“material”的定义,并且statFriction和dynFriction在当前上下文中不存在。是否需要为stat和dynFr定义一些变量iction?我需要写物理材料2D而不是材料吗?@HectorAstrom抱歉,我现在正在编辑它。(我的回答好像coll是一个
碰撞器,而不是
环形碰撞器2D
)是的,我基本上只是假设,对撞机对我的轮子来说意味着cirlce collider 2d,但这是一个错误的假设,我应该使用通用对撞机吗?圆形对撞机2d就是我上面描述的错误。我看到你编辑了你的原始帖子。这是我想设置物理材料的方式,但增加了唱车轮上的摩擦似乎没有任何作用。我知道材质的应用是正确的,就像我在游戏中更改弹跳度一样。但如果我更改摩擦,则不会更新,或者至少它不明显。@HectorAstrom我认为这是因为您的代码与对撞机的设置没有任何关系。
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
        }
    }