hey guys, so i have this cube as a player. i need it to be able to jump from one position to another. i wrote this script, and it works fine - unless the player moves before jumping.
the player can move (what is another script) flipping along the edges of the cube.
so, this code here is only working when starting the game and jumping without moving. as soon as the player moves and then jumps, he is sent to the strangest locations - but not where i need it to land after jumping.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationJumpScript : MonoBehaviour {
public AnimationCurve moveCurve;
public AnimationCurve jumpCurve;
public AnimationCurve flipCurve;
public MoveCube cubePositionObject;
private Vector3 startPos;
private Vector3 endPos;
private Vector3 targetAngle;
public Vector3 currentAngle;
private float lerpTime = 2;
private float currentLerpTime = 0;
private bool keyHit = false;
private void Start()
{
startPos = cubePositionObject.transform.position;
Math.Round(startPos.x, 0);
endPos = startPos + new Vector3(2, 0, 0);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("space"))
{
// set the positions needed for the jump animation
SetPositions();
keyHit = true;
}
if (keyHit == true) {
// the current angle of the cube
currentAngle = transform.eulerAngles;
currentLerpTime += Time.deltaTime * 1.5f;
if(currentLerpTime >= lerpTime)
{
currentLerpTime = lerpTime;
}
GetTargetAngle();
float perc = currentLerpTime / lerpTime;
// the movement of the cube
transform.position = Vector3.Lerp(startPos, endPos, moveCurve.Evaluate(perc));
// the jump of the cube
transform.position = Vector3.Lerp(transform.localPosition, startPos + Vector3.up, jumpCurve.Evaluate(perc));
// the rotation of the cube
currentAngle = new Vector3(
Mathf.LerpAngle(currentAngle.x, targetAngle.x, flipCurve.Evaluate(perc)),
Mathf.LerpAngle(currentAngle.y, targetAngle.y, flipCurve.Evaluate(perc)),
Mathf.LerpAngle(currentAngle.z, targetAngle.z, flipCurve.Evaluate(perc)));
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(currentAngle), flipCurve.Evaluate(perc));
OnJumpFinished();
}
}
private void SetPositions()
{
// the start position is the current position
startPos = cubePositionObject.transform.position;
// just to be sure
Math.Round(startPos.x, 0);
// end position is 2 clicks ahead on the x axis
endPos = startPos + new Vector3(2, 0, 0);
}
private void OnJumpFinished()
{
// check if the cube has reached its destination
if (cubePositionObject.transform.position == endPos)
{
keyHit = false;
SetTargetAngleLastTip(targetAngle);
// resetting the attributes
currentLerpTime = 0;
currentAngle = transform.eulerAngles;
GetTargetAngle();
}
}
private Vector3 GetTargetAngle()
{
// check the rotation the cube is at. if flipped, the target is to unflip it and vice versa.
// the angles need to be odd for the lerp to rotate in the right direction
Vector3 unflipped = new Vector3(0, 0, 0);
Vector3 flipped = new Vector3(0, 0, 180);
if (transform.eulerAngles == unflipped)
{
targetAngle = new Vector3(0, 0, -179);
}
if (transform.eulerAngles == flipped)
{
targetAngle = new Vector3(0, 0, 1);
}
return targetAngle;
}
private void SetTargetAngleLastTip(Vector3 currentAngle)
{
// the odd rotation when at the end of the lerp is corrected for a perfect 18Ăź degree flip
if (currentAngle == new Vector3(0, 0, -179))
{
Vector3 finishedAngle;
finishedAngle = new Vector3(0, 0, -180);
transform.eulerAngles = finishedAngle;
}
if (currentAngle == new Vector3(0, 0, 1))
{
Vector3 finishedAngle;
finishedAngle = new Vector3(0, 0, 0);
transform.eulerAngles = finishedAngle;
}
}
}
the positions are set correctly before the jump. i can only guess that theres something going on with the lerp stuff… but i am at the end of my knowledge. any help is greatly appreciated. ![]()