有没有unity大佬,求教!
unity3d吧
全部回复
仅看楼主
level 1
我刚学unity,但是就遇到问题,之前用rb.velocity实现跳跃没有问题,但是现在换成addforce却按了space键没有反应。
代码放不下,我在评论区里放吧
2024年05月17日 02点05分 1
level 5
[疑问]看看代码
2024年05月17日 05点05分 2
发了
2024年05月17日 08点05分
level 1
你把addforce力调大点试试
2024年05月17日 06点05分 3
不行呀,那个jumpforce我试过调很高的
2024年05月17日 08点05分
level 5
addforce数值加大一些...
2024年05月17日 07点05分 4
改第一个参数的vector2的值嘛,但是我试过给jumpforce调很高呀,还是不行
2024年05月17日 08点05分
level 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
public static PlayerController instance;
public Rigidbody2D rb;
public SpriteRenderer SR;
public Transform GroundCheckPoint;
public float moveSpeed;
public float JumpForce;
public float sprintForce;
public float dashSpeed = 15f; // 冲刺速度
public float dashDuration = 0.2f; // 冲刺持续时间
public float dashCooldown = 1f; // 冲刺冷却时间
private bool isDashing = false; // 是否正在冲刺
private float dashStartTime; // 冲刺开始时间
private bool canDash = true; // 是否可以冲刺
private bool canJump = true;
private bool isJumping;
public LayerMask whatIsGround;
public bool isGround;
private Animator animator;
Vector2 moveDir;
Vector2 moveAmount;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
SR = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
moveDir = new Vector2(Input.GetAxis("Horizontal"),0);
moveAmount = moveDir * moveSpeed * Time.deltaTime;
//网页链接 = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);//move to left and right
isGround = Physics2D.OverlapCircle(GroundCheckPoint.position, .2f, whatIsGround);//检测角色是否在地面
if (Input.GetButtonDown("Jump") && canJump)//跳跃
{
canJump = false;
isJumping = true;
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
{
}
if (moveDir.x > 0 )//flip sprite
{
网页链接 = false;
}else if(moveDir.x < 0 )
{
SR.flipX=true;
}
2024年05月17日 08点05分 7
canJump=false,isJumping=true了,然后呢,你addforce在哪
2024年05月17日 08点05分
哦,没看到你下面还有一段
2024年05月17日 08点05分
level 1
为什么发代码图片会被删
2024年05月17日 08点05分 10
level 1
animator.SetBool("isGrounded", isGround);
网页链接 ("isRun", 网页链接 (moveDir.x));
if(moveDir.y > 0 )
{
animator.SetFloat("isJumpUp",moveDir.y);
}
if(moveDir.y < 0 )
{
animator.SetFloat("isJumpDown", moveDir.y);
}
}
private void FixedUpdate()
{
if(isJumping)
{
rb.AddForce(网页链接 * JumpForce, ForceMode2D.Impulse);
Debug.Log("enter fix");
Debug.Log(rb.velocity);
isJumping=false;
}
rb.MovePositio(rb.position+ moveDir * time.deltatime* moveSpeed);
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground") && !canJump)
{
canJump = true;
isGround = true;
isJumping = false;
}
}
}
2024年05月17日 08点05分 11
我怀疑是因为你那个检测地面的object在你人物的碰撞箱下面一点的位置,然后在人物跳跃之后人物碰撞箱离地但那个object没离地,导致isJumping又变成false了
2024年05月17日 08点05分
这种问题我也遇到过
2024年05月17日 08点05分
@多多良.小伞 不对呀,我开调试模式,按好几下,那个isjumping才会变,而且只是变一瞬间,就又变false了
2024年05月18日 07点05分
level 5
你在按跳跃键的时候看一下那几个变量的变化情况
2024年05月17日 08点05分 12
level 3
啊,根本原因是你在给moveDir赋值时,将y的值写死为了0
2024年05月18日 19点05分 14
level 3
moveDir = new Vector2(Input.GetAxis("Horizontal"),0);
中的0改为rb.velocity.y,也就是
moveDir = new Vector2(Input.GetAxis("Horizontal"),rb.velocity.y);
就可以跳了
2024年05月18日 19点05分 15
建议你用rb.velocity = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);来实现移动,会简单一点。MovePosition感觉有点小问题,不过只是个人建议啦
2024年05月18日 19点05分
1