The problem is that running the code below, the “jump” bool never becomes “true”.
I am using the Debug.Log to check that the ground distance is bigger than “0” and it is when I make the click and the object moves, but the bool is still “false”, and just in a few milliseconds the objects just goes back down. It never completes the Lerp.
If I take out the statement:
if (gd == 0.0f)
{
jump = false;
}
… it works. But ofcourse, I can never set “jump” to false that way.
Well, I suppose it is something I’m overlooking but, I just can’t see what. Or maybe just my logic is crap ![]()
Please, can anyone tell me what is wrong here.
using UnityEngine;
using System.Collections;
public class MoveControl : MonoBehaviour {
private bool jump;
private float distance = 4.5f;
private float time = 0.15f;
private float gd;
private float yVelocity = 0.0f;
private float gravity = 9.81f;
private Vector3 target;
private Vector3 point;
void Start()
{
point = transform.position;
jump = false;
}
void Update()
{
gd = GroundDistance();
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
point = ray.origin + (ray.direction * distance);
point.z = 0.0f;
jump = true;
}
if(jump)
{
transform.position = new Vector3((Mathf.Lerp(transform.position.x, point.x, time)), (Mathf.Lerp(transform.position.y, point.y, time)), transform.position.z);
}
if(gd > 0.0f)
{
yVelocity -= gravity * Time.deltaTime;
transform.position = new Vector3(transform.position.x, (transform.position.y + (yVelocity * Time.deltaTime)), transform.position.z);
}
if (gd == 0.0f)
{
jump = false;
}
Debug.Log("Jump: " + jump);
Debug.Log(gd);
}
public float GroundDistance()
{
RaycastHit hit;
Physics.Raycast(transform.position, Vector3.down, out hit, 10.0f);
// Debugging line representing the Raycast ray, only in editor mode for development
Debug.DrawLine(transform.position, hit.point, Color.red, 1.0f, false);
return hit.distance;
}
}