Итак, базойси мне нужен мой персонаж, чтобы подняться, когда нажаты space
или mousebutton
. Я добавил rigbody 2d и box colider 2d в свой gameobject
(плеер). Проблема в том, что когда я ударил space
или mousebutton
, он прыгал не постоянно вверх.UNITY 2D: Игрок, летящий, когда космос/мышь удерживается
Вот мой код:
using UnityEngine;
using System.Collections;
public class PixelMovement : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public Vector3 PressVelocity;
public float maxSpeed = 5f;
public float fowardSpeed = 1f;
public float jumpForce;
bool didPress = false;
// Use this for initialization
void Start() {
}
//Do Graphic & Input updates
void Update()
{
if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
{
didPress = true;
}
}
//Do physics engine updates here
void FixedUpdate()
{
velocity.x = fowardSpeed;
if (didPress == true)
{
didPress = false;
velocity += PressVelocity * Time.deltaTime *jumpForce;
}
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
}
}
Я пробовал свой код и отлично работает на своем компьютере. [Здесь я оставил ссылку с видео] (https://youtu.be/-JzNxQdYJMs). Есть ли у вас другой скрипт, прикрепленный к этому GameObject? – ORParga
Это '2d' не' 3d' – pHenomen
Я прикрепил 'box colider 2d' и снял флажок' срабатывает', а также добавил 'rigbody2d' – pHenomen