Столкнулся с такой проблемой:
Есть код с управлением персонажа, его нужно доработать(добавить "Рывок")
Я нашёл код для рывка , но я не могу их совместить (только учусь).
В этом коде идёт проверка: под персонажем есть невидимый box collider и если он касается земли то прыжок доступен.(Видел видео о том как сделать прыжок для персонажа)
В моём идёт проверка по тегу "Ground".
Мой код:
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]
public class Move : MonoBehaviour
{
// Переменные для рывка
public float maxDashTime = 2f;
public float dashSpeed = 150f;
float currentDashSpeed;
float currentDashTime;
//
public enum ProjectAxis { onlyX = 0, xAndY = 1 };
private ProjectAxis projectAxis = ProjectAxis.onlyX;
private float speed = 150;
private float addForce = 9;
private KeyCode leftButton = KeyCode.A; //Управление
private KeyCode rightButton = KeyCode.D;
private KeyCode downButton = KeyCode.S;
private KeyCode Jump = KeyCode.W;
public KeyCode Spurt = KeyCode.F;
public bool isFacingRight = true; //Проверка по x
private Vector3 direction;
private float vertical;
private float horizontal;
private Rigidbody2D body;
private float rotationY;
private bool jump;
Animator anim; //Аниматор для персонажа
void Start()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
body.fixedAngle = true;
if (projectAxis == ProjectAxis.xAndY)
{
body.gravityScale = 0;
body.drag = 10;
}
}
void OnCollisionStay2D(Collision2D coll)
{
if (coll.transform.tag == "Ground")
{
anim.SetBool("jump", false);
body.drag = 5;
jump = true;
}
}
void OnCollisionExit2D(Collision2D coll)
{
if (coll.transform.tag == "Ground")
{
anim.SetBool("jump", true);
body.drag = 0;
jump = false;
}
}
void FixedUpdate()
{
body.AddForce(direction * body.mass * speed);
if (Mathf.Abs(body.velocity.x) > speed / 100f)
{
body.velocity = new Vector2(Mathf.Sign(body.velocity.x) * speed / 100f, body.velocity.y);
}
if (projectAxis == ProjectAxis.xAndY)
{
if (Mathf.Abs(body.velocity.y) > speed / 100f)
{
body.velocity = new Vector2(body.velocity.x, Mathf.Sign(body.velocity.y) * speed / 100f);
}
}
else
{
if (Input.GetKey(Jump) && jump)
{
body.velocity = new Vector2(0, addForce);
}
}
}
void Flip()
{
if (projectAxis == ProjectAxis.onlyX)
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.F)) //Начало кода для рывка
{
currentDashTime = 0.0f;
}
if (currentDashTime < maxDashTime)
{
currentDashSpeed = dashSpeed;
currentDashTime += Time.deltaTime;
}
else
{
currentDashSpeed = 0.0f;
}
Vector3 moveDir = Vector3.zero;
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") + currentDashSpeed); //Конец кода для рывка
// тут должно быть controller.Move(moveDir * Time.deltaTime);
if (Input.GetKey(leftButton)) horizontal = -1;
else if (Input.GetKey(rightButton)) horizontal = 1; else horizontal = 0;
if (Input.GetKeyDown(KeyCode.S))
{
anim.SetBool("down", true);
}
else
{
anim.SetBool("down", false);
}
if (projectAxis == ProjectAxis.onlyX)
{
direction = new Vector2(horizontal, 0);
}
else
{
if (Input.GetKeyDown(Jump)) speed += addForce; else if (Input.GetKeyUp(Jump)) speed -= addForce;
direction = new Vector2(horizontal, vertical);
}
if (horizontal > 0 && !isFacingRight) Flip(); else if (horizontal < 0 && isFacingRight) Flip();
}
}
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]
public class Move : MonoBehaviour
{
// Переменные для рывка
public float maxDashTime = 2f;
public float dashSpeed = 150f;
float currentDashSpeed;
float currentDashTime;
//
public enum ProjectAxis { onlyX = 0, xAndY = 1 };
private ProjectAxis projectAxis = ProjectAxis.onlyX;
private float speed = 150;
private float addForce = 9;
private KeyCode leftButton = KeyCode.A; //Управление
private KeyCode rightButton = KeyCode.D;
private KeyCode downButton = KeyCode.S;
private KeyCode Jump = KeyCode.W;
public KeyCode Spurt = KeyCode.F;
public bool isFacingRight = true; //Проверка по x
private Vector3 direction;
private float vertical;
private float horizontal;
private Rigidbody2D body;
private float rotationY;
private bool jump;
Animator anim; //Аниматор для персонажа
void Start()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
body.fixedAngle = true;
if (projectAxis == ProjectAxis.xAndY)
{
body.gravityScale = 0;
body.drag = 10;
}
}
void OnCollisionStay2D(Collision2D coll)
{
if (coll.transform.tag == "Ground")
{
anim.SetBool("jump", false);
body.drag = 5;
jump = true;
}
}
void OnCollisionExit2D(Collision2D coll)
{
if (coll.transform.tag == "Ground")
{
anim.SetBool("jump", true);
body.drag = 0;
jump = false;
}
}
void FixedUpdate()
{
body.AddForce(direction * body.mass * speed);
if (Mathf.Abs(body.velocity.x) > speed / 100f)
{
body.velocity = new Vector2(Mathf.Sign(body.velocity.x) * speed / 100f, body.velocity.y);
}
if (projectAxis == ProjectAxis.xAndY)
{
if (Mathf.Abs(body.velocity.y) > speed / 100f)
{
body.velocity = new Vector2(body.velocity.x, Mathf.Sign(body.velocity.y) * speed / 100f);
}
}
else
{
if (Input.GetKey(Jump) && jump)
{
body.velocity = new Vector2(0, addForce);
}
}
}
void Flip()
{
if (projectAxis == ProjectAxis.onlyX)
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.F)) //Начало кода для рывка
{
currentDashTime = 0.0f;
}
if (currentDashTime < maxDashTime)
{
currentDashSpeed = dashSpeed;
currentDashTime += Time.deltaTime;
}
else
{
currentDashSpeed = 0.0f;
}
Vector3 moveDir = Vector3.zero;
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") + currentDashSpeed); //Конец кода для рывка
// тут должно быть controller.Move(moveDir * Time.deltaTime);
if (Input.GetKey(leftButton)) horizontal = -1;
else if (Input.GetKey(rightButton)) horizontal = 1; else horizontal = 0;
if (Input.GetKeyDown(KeyCode.S))
{
anim.SetBool("down", true);
}
else
{
anim.SetBool("down", false);
}
if (projectAxis == ProjectAxis.onlyX)
{
direction = new Vector2(horizontal, 0);
}
else
{
if (Input.GetKeyDown(Jump)) speed += addForce; else if (Input.GetKeyUp(Jump)) speed -= addForce;
direction = new Vector2(horizontal, vertical);
}
if (horizontal > 0 && !isFacingRight) Flip(); else if (horizontal < 0 && isFacingRight) Flip();
}
}
Код который я нашёл для "рывка":
Синтаксис:
Используется csharp
public CharacterController controller;
// Переменный для рывка
public float maxDashTime = 2f;
public float dashSpeed = 150f;
float currentDashSpeed;
float currentDashTime;
void Update() {
if (Input.GetKeyDown(KeyCode.F)) {
currentDashTime = 0.0f;
}
if (currentDashTime < maxDashTime) {
currentDashSpeed = dashSpeed;
currentDashTime += Time.deltaTime;
} else {
currentDashSpeed = 0.0f;
}
Vector3 moveDir = Vector3.zero;
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") + currentDashSpeed);
controller.Move(moveDir * Time.deltaTime);
}
// Переменный для рывка
public float maxDashTime = 2f;
public float dashSpeed = 150f;
float currentDashSpeed;
float currentDashTime;
void Update() {
if (Input.GetKeyDown(KeyCode.F)) {
currentDashTime = 0.0f;
}
if (currentDashTime < maxDashTime) {
currentDashSpeed = dashSpeed;
currentDashTime += Time.deltaTime;
} else {
currentDashSpeed = 0.0f;
}
Vector3 moveDir = Vector3.zero;
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") + currentDashSpeed);
controller.Move(moveDir * Time.deltaTime);
}
Проблема в
controller.Move(moveDir * Time.deltaTime);
у меня он не используется нигде поэтому не работает. Если эту строку вырезать, то ничего не работает
Помогите пожалуйста. Заранее спасибо
Вот ещё ссылка на ту тему где нашёл код:
http://www.cyberforum.ru/unity/thread2207383.html