Страница 1 из 1

Как получить скорость объекта float?

СообщениеДобавлено: 06 май 2022, 10:21
_MrKleshch_
Как получить скорость объекта float? Мне нужно значение от 0 до 1(максимальной скорости) для анимации.

Re: Как получить скорость объекта float?

СообщениеДобавлено: 06 май 2022, 14:46
_MrKleshch_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class PlayerCon : MonoBehaviour
{
public PhotonView view;
private Rigidbody rb;
private Animator animator;
[Header("Скорость перемещения персонажа")]
public float speed = 7f;
[Header("Скорость бега")]
public float runSpeed = 15f;
[Header("Мы на земле?")]
public bool ground;
[Header("Сила прыжка")]
public float jumpPower = 200f;

private void Awake()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}

void Update()
{
GetInput();
}
private void GetInput()
{
if (Input.GetKey(KeyCode.W))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += transform.forward * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
}
if (Input.GetKey(KeyCode.S))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += -transform.forward * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += -transform.forward * speed * Time.deltaTime;
}
}
if (Input.GetKey(KeyCode.A))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += -transform.right * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += -transform.right * speed * Time.deltaTime;
}
}
if (Input.GetKey(KeyCode.D))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += transform.right * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += transform.right * speed * Time.deltaTime;
}
}

if (Input.GetKeyDown(KeyCode.Space))
{
if(ground == true)
{
rb.AddForce(transform.up * jumpPower);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
ground = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
ground = false;
}
}
}

Re: Как получить скорость объекта float?

СообщениеДобавлено: 06 май 2022, 14:52
_MrKleshch_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class PlayerCon : MonoBehaviour
{
public PhotonView view;
private Rigidbody rb;
private Animator animator;
[Header("Скорость перемещения персонажа")]
public float speed = 7f;
[Header("Скорость бега")]
public float runSpeed = 15f;
[Header("Мы на земле?")]
public bool ground;
[Header("Сила прыжка")]
public float jumpPower = 200f;

private void Awake()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}

void Update()
{
GetInput();
}
private void GetInput()
{
if (Input.GetKey(KeyCode.W))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += transform.forward * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
}
if (Input.GetKey(KeyCode.S))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += -transform.forward * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += -transform.forward * speed * Time.deltaTime;
}
}
if (Input.GetKey(KeyCode.A))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += -transform.right * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += -transform.right * speed * Time.deltaTime;
}
}
if (Input.GetKey(KeyCode.D))
{
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localPosition += transform.right * runSpeed * Time.deltaTime;
}
else
{
transform.localPosition += transform.right * speed * Time.deltaTime;
}
}

if (Input.GetKeyDown(KeyCode.Space))
{
if(ground == true)
{
rb.AddForce(transform.up * jumpPower);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
ground = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
ground = false;
}
}
}

Re: Как получить скорость объекта float?

СообщениеДобавлено: 06 май 2022, 19:18
Alkos26Rus
текущая скорость / максимальная скорость