public class CharacterController2D : MonoBehaviour { // Move player in 2D space public float maxSpeed = 3.4f; public float jumpHeight = 6.5f; public float gravityScale = 1.5f; public Camera mainCamera; public int Fuel = 10; public UnityEngine.UI.Image image;
Создал UI Image Сделал тип filled, но как сделать так, чтобы при нажатии пробела уменьшалась графа топлива image.fillAmount = Fuel; незнаю куда сувать, и не работает. Помогите пожалуйтса
Не ответ, а скорее совет. Тебе нужен фон и сама полоска для топлива. В зависимости от процента топлива, уменьшай размер полоски через трансформацию спрайта. Либо сделай проще и выводи значение топлива через цифру.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(CapsuleCollider2D))]
public class CharacterController2D : MonoBehaviour
{
// Move player in 2D space
public float maxSpeed = 3.4f;
public float jumpHeight = 6.5f;
public float gravityScale = 1.5f;
public Camera mainCamera;
public int Fuel = 10;
public UnityEngine.UI.Image image;
bool facingRight = true;
float moveDirection = 0;
bool isGrounded = false;
Vector3 cameraPos;
Rigidbody2D r2d;
Collider2D mainCollider;
// Check every collider except Player and Ignore Raycast
LayerMask layerMask = ~(1 << 2 | 1 << 8);
Transform t;
// Use this for initialization
void Start()
{
t = transform;
r2d = GetComponent();
mainCollider = GetComponent();
r2d.freezeRotation = true;
r2d.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
r2d.gravityScale = gravityScale;
facingRight = t.localScale.x > 0;
gameObject.layer = 8;
if(mainCamera)
cameraPos = mainCamera.transform.position;
}
// Update is called once per frame
void Update()
{
moveDirection = 1;
// Change facing direction
if (moveDirection != 0)
{
if (moveDirection > 0 && !facingRight)
{
facingRight = true;
t.localScale = new Vector3(Mathf.Abs(t.localScale.x), t.localScale.y, transform.localScale.z);
}
}
// Jumping
if (Input.GetKeyDown("space"))
{
if (Fuel > 0){
r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight);
Fuel-=1;
}
}
// Camera follow
if(mainCamera)
mainCamera.transform.position = new Vector3(t.position.x, cameraPos.y, cameraPos.z);
}
void FixedUpdate()
{
Bounds colliderBounds = mainCollider.bounds;
Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, 0.1f, 0);
// Check if player is grounded
isGrounded = Physics2D.OverlapCircle(groundCheckPos, 0.23f, layerMask);
// Apply movement velocity
r2d.velocity = new Vector2((moveDirection) * maxSpeed, r2d.velocity.y);
// Simple debug
Debug.DrawLine(groundCheckPos, groundCheckPos - new Vector3(0, 0.23f, 0), isGrounded? Color.green : Color.red);
}
}
Создал UI Image Сделал тип filled, но как сделать так, чтобы при нажатии пробела уменьшалась графа топлива
image.fillAmount = Fuel; незнаю куда сувать, и не работает. Помогите пожалуйтса