【发布时间】:2020-06-15 11:27:08
【问题描述】:
我对 Unity 和 C# 都非常陌生,并且已经研究了几天。我目前正试图阻止我的播放器滑动,为此我将播放器材料的摩擦值设置得很高,这样它就不会滑动。然而,这会产生一个问题,即我的角色移动速度过快。为了解决这个问题,我创建了一个带有 BoxCollider2D 的子对象,标记为摩擦控制器,我可以对其进行修改。我得到一个代码,当我开始移动时将物理材料的摩擦值更改为 0,当我应该停止时更改为 100。问题是虽然这会更新材质本身,但它不会影响盒子碰撞器设置。有人知道这个的解决方案吗?
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
GameObject frictionController;
public BoxCollider2D collider;
public float speed = 400f;
public float jumpForce;
private float friction;
private Rigidbody2D rb2d;
private bool isMoving;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(moveHorizontal,0);
rb2d.AddForce(movement * speed);
}
void Update()
{
frictionController = GameObject.FindWithTag("Friction Controller");
collider = frictionController.GetComponent<BoxCollider2D>();
if (Input.GetKey("a") || (Input.GetKey("d")))
{
{ Debug.Log("Pressed Button"); }
collider.sharedMaterial.friction = 0;
} else { collider.sharedMaterial.friction = 100; }
///This part isn't complete yet
float moveVertical = Input.GetAxis("Vertical");
Vector2 jump = new Vector2(0, moveVertical);
if (Input.GetKeyDown("space"))
{
rb2d.AddForce(Vector3.up * jumpForce);
}
}
}
【问题讨论】:
-
是否有可能因为
Input.GetAxis的缓动而滑动?尝试改用Input.GetAxisRaw -
那没用