访问手机版

扫描体验手机版

游客您好
第三方账号登陆
  • 点击联系客服

    在线时间:8:00-24:00

    客服QQ

    706393200

    电子邮件

    kninebox@163.com
  • KNINEBOX刀盒APP

    即将上线,敬请期待

  • 扫描二维码

    进入手机版

推荐阅读 更多
Lv.2
第25号会员,11活跃度,2020.08.25加入
  • 138发帖
  • 133主题
  • 0关注
  • 0粉丝
这个人很懒,什么也没有留下。
最近评论
热门专题

[教程相关] 有没有unity大佬,求教!

[复制链接]
乔微博 发表于 2024-5-18 10:01:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
我刚学unity,但是就遇到问题,之前用rb.velocity实现跳跃没有问题,但是现在换成addforce却按了space键没有反应。
代码放不下,我在评论区里放吧

有没有unity大佬,求教!-1.jpg

有没有unity大佬,求教!-2.jpg

有没有unity大佬,求教!-3.jpg
回复

使用道具 举报

精彩评论7

123456879 发表于 2024-5-18 10:01:25 | 显示全部楼层
有没有unity大佬,求教!-1.png 看看代码
回复

使用道具 举报

麻辣鸡翅 发表于 2024-5-18 10:02:11 | 显示全部楼层
你把addforce力调大点试试
回复

使用道具 举报

天一教育培训寐 发表于 2024-5-18 10:02:35 | 显示全部楼层
addforce数值加大一些...
回复

使用道具 举报

 楼主| 乔微博 发表于 2024-5-18 10:02:43 | 显示全部楼层
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:07 | 显示全部楼层
为什么发代码图片会被删
回复

使用道具 举报

 楼主| 乔微博 发表于 2024-5-18 10:04:03 | 显示全部楼层
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:00 | 显示全部楼层
你在按跳跃键的时候看一下那几个变量的变化情况
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

本站发布的mod均为免费mod以及本站原创mod,所有作品仅供交流和艺术拍照使用,请勿使用本站捏脸数据拍摄和制作国家法律法规禁止的内容发布到网络。本站捏脸数据及mod不作为商业用途,仅以赞助钻石的形式作为大家对作者手工劳动的认可。

声明:本站部分内容来自网络站点所提供的公开引用资源,作品版权归原作者所有 禁止商用 违者必究。

Powered by Discuz! X3.4© 2001-2013 Comsenz Inc. KNINEBOX