Пишу простой раннер, игрок двигается в горизонтальной плоскости, должен прыгать и попадать на платформы, все состоит из префаба куба, возникли проблемы с физикой, Когда нажимаешь пробел(прыжок) объект Runner не прыгает, а бывает прыгает, также бывает прыжок, сильнее обычного, еще бывает при движении по платформе, он стопорит. На материале убрал по 0 все сопротивления,
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Runner : MonoBehaviour
{
// Start is called before the first frame update
public static float distanceTraveled;
public Rigidbody rb;
public float speed;
private bool touchingPlatform;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
distanceTraveled = transform.localPosition.x;
if (rb.IsSleeping())
{
Debug.Log("Спит");
rb.WakeUp();
//rb.velocity = new Vector3(2.0f, 0.0f, 0.0f) * speed;
}
/*
if (rb.velocity.x < 1.0f)
//rb.velocity = new Vector3(5.0f, 5.0f, 0.0f) * speed;
{
}
*/
if (Input.GetKeyDown(KeyCode.Space))
{
if (touchingPlatform)
{
rb.AddForce(new Vector3(3.0f,5.0f, 0.0f), ForceMode.VelocityChange);
return;
}
}
if (touchingPlatform)
{
//Debug.Log(rb.velocity);
rb.AddForce(new Vector3(3.0f, 0.0f, 0.0f) * speed, ForceMode.Force);
if (rb.velocity.x > 6.0f)
{
rb.velocity = new Vector3(5.0f, 0.0f, 0.0f);
}
//touchingPlatform = false;
}
}
void OnCollisionEnter()
{
touchingPlatform = true;
}
void OnCollisionExit()
{
touchingPlatform = false;
}
}
using System.Collections.Generic;
using UnityEngine;
public class Runner : MonoBehaviour
{
// Start is called before the first frame update
public static float distanceTraveled;
public Rigidbody rb;
public float speed;
private bool touchingPlatform;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
distanceTraveled = transform.localPosition.x;
if (rb.IsSleeping())
{
Debug.Log("Спит");
rb.WakeUp();
//rb.velocity = new Vector3(2.0f, 0.0f, 0.0f) * speed;
}
/*
if (rb.velocity.x < 1.0f)
//rb.velocity = new Vector3(5.0f, 5.0f, 0.0f) * speed;
{
}
*/
if (Input.GetKeyDown(KeyCode.Space))
{
if (touchingPlatform)
{
rb.AddForce(new Vector3(3.0f,5.0f, 0.0f), ForceMode.VelocityChange);
return;
}
}
if (touchingPlatform)
{
//Debug.Log(rb.velocity);
rb.AddForce(new Vector3(3.0f, 0.0f, 0.0f) * speed, ForceMode.Force);
if (rb.velocity.x > 6.0f)
{
rb.velocity = new Vector3(5.0f, 0.0f, 0.0f);
}
//touchingPlatform = false;
}
}
void OnCollisionEnter()
{
touchingPlatform = true;
}
void OnCollisionExit()
{
touchingPlatform = false;
}
}