【发布时间】:2021-09-04 04:44:13
【问题描述】:
我将我的角色粘贴到一个空对象上,然后将 Rigidbody、脚本应用于该空对象(更改轴心)。
使用 System.Collections; System.Collections.Generic;统一引擎;
公共类 main_character : MonoBehaviour { 公共浮动速度;
private float max_vel = 4f;
private bool grounded = true;
private bool flip = true;
private Rigidbody2D main_vel;
private void Awake()
{
main_vel = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
float now_vel = Mathf.Abs(main_vel.velocity.x);
if (Input.GetKey("d") && flip)
{
flip_side();
if (now_vel < max_vel)
{
main_vel.AddForce(new Vector2(speed, 0f) * Time.deltaTime);
}
}
if(Input.GetKey("a") && !flip)
{
!flip_side();
if (now_vel < max_vel)
{
main_vel.AddForce(new Vector2(-speed, 0f) * Time.deltaTime);
}
}
}
void flip_side()
{
flip = !flip;
transform.Rotate(0f, 180f, 0f);
}
}
【问题讨论】:
-
您似乎从未设置过速度值。您只需声明变量,这意味着它将具有默认值 0。
-
@HumanWrites 因为它是公开的,所以它是序列化的,您可以通过 Unity 中的 Inspector 编辑值