有没有大佬指点一下,小白,刚学两天,我跟着教程一步一步做的,但是一点开始就自动移动了,按着方向机都纠正不过来,要按着W或者S才能按AD,松开又自动往左走了
unity3d吧
全部回复
仅看楼主
level 4
Uezawa_文吾 楼主


2024年08月28日 14点08分 1
level 4
Uezawa_文吾 楼主
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public PlayerInputControl inputControl;
public Vector2 inputDirection;
public float speed;
private Rigidbody2D rb;
private void Awake()
{
inputControl = new PlayerInputControl(); // 注意:这里可能不是最佳实践,通常Unity的Input System是通过Inspector赋值
rb = GetComponent<Rigidbody2D>();
inputDirection = Vector2.zero; // 初始化inputDirection
rb = GetComponent<Rigidbody2D>();
}
private void OnEnable()
{
inputControl.Enable();
}
private void OnDisable()
{
inputControl.Disable();
}
private void Update()
{
inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();
}
private void FixedUpdate()
{
Move();
}
public void Move()
{
网页链接 = new Vector2(speed * inputDirection.x * 网页链接 , rb.velocity.y);
int faceDir = (int)transform.localScale.x;
if (inputDirection.x > 0)
faceDir = 1;
if (inputDirection.x < 0)
faceDir = -1;
网页链接 = new Vector3(faceDir, 1, 1);
}
}
2024年08月28日 15点08分 2
level 4
Uezawa_文吾 楼主
代码是这样的
2024年08月28日 15点08分 3
level 6
你那代码里的网页链接是什么,还有检查配置上下左右是不是设置错了。
2024年08月29日 02点08分 4
大佬你再看看我评论区新发的图片,谢谢你
2024年08月30日 08点08分
我上下左右是用组件生成的哎
2024年08月30日 08点08分
@Uezawa_文吾 你这教程是搁哪儿看的。。。
2024年08月30日 09点08分
level 4
Uezawa_文吾 楼主
2024年08月30日 08点08分 5
看代码不至于自动移动,打印出来inputDirection的值看看,或者调试看看也行,最后再看看我上面说的那个配置的Up,Down,Left,Right配置是啥样的,不行你就截个图。 代码的其他问题:刚体的velocity是速度向量理论上应该不需要乘以deltaTime,要乘的话再FixedUpdate下调用也是乘以fixedDeltaTime。
2024年08月30日 09点08分
level 5
这个应该是根据bilibili上那个台湾人做的,你的player .cs 估计里面的代码没写对
2025年11月07日 16点11分 6
1