KNINEBOX捏脸学院
标题:
有没有unity大佬,求教!
[打印本页]
作者:
乔微博
时间:
2024-5-18 10:01
标题:
有没有unity大佬,求教!
我刚学unity,但是就遇到问题,之前用rb.velocity实现跳跃没有问题,但是现在换成addforce却按了space键没有反应。
代码放不下,我在评论区里放吧
(, 下载次数: 0)
上传
点击文件名下载附件
(, 下载次数: 0)
上传
点击文件名下载附件
(, 下载次数: 0)
上传
点击文件名下载附件
作者:
123456879
时间:
2024-5-18 10:01
(, 下载次数: 0)
上传
点击文件名下载附件
看看代码
作者:
麻辣鸡翅
时间:
2024-5-18 10:02
你把addforce力调大点试试
作者:
天一教育培训寐
时间:
2024-5-18 10:02
addforce数值加大一些...
作者:
乔微博
时间:
2024-5-18 10:02
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-5-18 10:03
为什么发代码图片会被删
作者:
乔微博
时间:
2024-5-18 10:04
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;
}
}
}
作者:
123456879
时间:
2024-5-18 10:05
你在按跳跃键的时候看一下那几个变量的变化情况
欢迎光临 KNINEBOX捏脸学院 (https://www.kninebox.com/)
Powered by Discuz! X3.4