- Home /
Dash Problem Not Being Consistent With Travel Distance
I have a dash script for my rigid body character. It works fine but the problem is that the travel distance is not consistent. If the object is static the travel distance is short, but when the in motion the travel distance doubles. How can I fix this so that they can match. I hope someone can help me with this. Thanks in advance.
Here's to further illustrate my problem.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Vector3 jump;
public float jumpForce = 2.0f;
public float speed = 5f;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float airControlPercent;
public VirtualJoystick moveJoystick;
public float terminalRotationSpeed = 25.0f;
public bool isGrounded;
public bool foo;
Transform cameraT;
private Rigidbody rb;
private Animator anim;
void OnCollisionStay()
{
isGrounded = true;
}
private void Start()
{
cameraT = Camera.main.transform;
rb = GetComponent<Rigidbody> ();
anim = GetComponent<Animator> ();
jump = new Vector3(0.0f, 1.0f, 0.0f);
foo = false;
}
public void FixedUpdate()
{
// input
Vector3 input = new Vector2 (moveJoystick.Horizontal(), moveJoystick.Vertical());
Vector3 inputDir = input.normalized;
// Movement
Move (inputDir);
// Jump
if(Input.GetKeyDown(KeyCode.Space))
{
Jump ();
}
}
public void Move(Vector2 inputDir)
{
if (inputDir != Vector2.zero)
{
float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle (transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime (turnSmoothTime));
rb.AddForce (transform.forward * speed);
}
}
public void Jump()
{
if (isGrounded)
{
rb.AddForce (transform.forward * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
float GetModifiedSmoothTime(float smoothTime)
{
if (foo = true)
{
return smoothTime;
}
if (airControlPercent == 0) {
return float.MaxValue;
}
return smoothTime / airControlPercent;
}
}
static-min.png
(115.3 kB)
Comment