【发布时间】:2020-10-09 03:39:13
【问题描述】:
我的统一项目需要帮助。我是 gamedev 的新手,为了学习一些基础知识,我决定暴力破解马里奥游戏。然而,我已经开始制作一个动作脚本了。我很轻松地想出了如何让我的马里奥角色左右移动,但我的跳跃有问题。我可以跳,但我就是不知道如何进行地面探测。我会包含一些代码示例,但是我尝试了很多东西,我什至不知道我会包含什么。请花时间通过粘贴指向您的运动脚本副本的链接来帮助我,这样我就可以解决这个问题。谢谢!
编辑: 它仍然无法正常工作,我真的不知道为什么 这是我的代码:
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class Mario : MonoBehaviour
{
Rigidbody2D rb;
Animator animator;
SpriteRenderer spriterenderer;
bool pushingButton;
bool isGrounded;
[SerializeField]
float speed;
[SerializeField]
float jumpForce;
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
spriterenderer = GetComponent<SpriteRenderer>();
}
void FixedUpdate()
{
isGrounded = Physics2D.Linecast(_transform.position, GroundChecker.postion, whatIsFloor);
if (Input.GetKey("right") || Input.GetKey("left") || Input.GetKey("up"))
{
pushingButton = true;
}
else
pushingButton = false;
if (Input.GetKey("left") && Input.GetKey("right"))
{
animator.Play("Mario Idle");
rb.velocity = new Vector2(0, 0);
}
else if (Input.GetKey("right") && Input.GetKey("up"))
{
animator.Play("Mario Jump");
spriterenderer.flipX = false;
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else if (Input.GetKey("right"))
{
animator.Play("Mario Walk");
spriterenderer.flipX = false;
rb.velocity = new Vector2(speed, rb.velocity.y);
}
if (Input.GetKey("left") && Input.GetKey("right"))
{
animator.Play("Mario Idle");
rb.velocity = new Vector2(0, 0);
}
else if (Input.GetKey("left") && Input.GetKey("up"))
{
animator.Play("Mario Jump");
spriterenderer.flipX = true;
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
else if (Input.GetKey("left"))
{
animator.Play("Mario Walk");
spriterenderer.flipX = true;
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
if (Input.GetKeyDown("up") && isGrounded)
{
animator.Play("Mario Jump");
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
isGrounded = false;
}
if (pushingButton == false)
{
animator.Play("Mario Idle");
rb.velocity = new Vector2(0, rb.velocity.y);
}
}
}
【问题讨论】:
-
我们无法回答这个问题,除非您向我们展示您当前拥有的代码,无论代码有多损坏,因为我们不知道您是如何在游戏中实现玩家移动的。
标签: c# unity3d scripting game-development