当我不给它任何输入时,我如何增加汽车的摩擦力使其不会滑行?
How would I increase the friction of a car so that it doesn't glide when I don't give it any inputs?
各位。我是 Unity 2D 和 C# 的新手,我想知道如何在我不想要的时候阻止我的车在我的游戏中滑行和向前滑动,因为如果我真的感觉我无法控制我的车它滑得太多了。我希望如此,如果我不给汽车任何输入,或者不给汽车几乎立即停止或开始以其他方式移动的计数器输入。我已经尝试创建一个 Physics2D material 并增加摩擦力,但它对游戏玩法没有任何影响。我也试过检查,如果没有玩家输入,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);
}
}
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);
}
大家好,浏览过这个有类似问题的人。我最终解决这个问题的方法是在此处实现整个代码块:
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
}
}
我添加了评论,希望您能理解我做了什么以及为什么这样做。我希望这可以帮助任何遇到类似问题的人。
各位。我是 Unity 2D 和 C# 的新手,我想知道如何在我不想要的时候阻止我的车在我的游戏中滑行和向前滑动,因为如果我真的感觉我无法控制我的车它滑得太多了。我希望如此,如果我不给汽车任何输入,或者不给汽车几乎立即停止或开始以其他方式移动的计数器输入。我已经尝试创建一个 Physics2D material 并增加摩擦力,但它对游戏玩法没有任何影响。我也试过检查,如果没有玩家输入,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);
}
}
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);
}
大家好,浏览过这个有类似问题的人。我最终解决这个问题的方法是在此处实现整个代码块:
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
}
}
我添加了评论,希望您能理解我做了什么以及为什么这样做。我希望这可以帮助任何遇到类似问题的人。