- Home /
Transition Free Fall into Sinus Hovering
I have a character that should hover above a platform with a nice sinus based up and down movement.
Now the character jumps to this platform from above. I dont know from which height he jumps, so the his fall speed is variable. How would I compute that transition without my character touching the platform?
I'm thinking about this for a long time now, some help would make my day! :)
Answer by Rod-Green · Dec 30, 2011 at 01:07 AM
You can't store the last hover height before the jump and include that into the jump arc calculation?
Yes I can, but how would I calculate the jump arc? Its the mathematical part where I'm stuck.
Answer by Rod-Green · Dec 30, 2011 at 07:29 PM
In that case here's a more complete answer:
using System.Collections.Generic;
using UnityEngine;
public class Jumper : MonoBehaviour
{
public float m_upwardsMomentum = 200;
public float m_forwardMomentum = 100;
public Transform m_target;
public bool m_doJump;
public float m_gravity = 9.8f;
public float m_terminalVelocityTime = 1.0f;
public float m_minUpdate = 0.033333f;
float m_lastUpdate;
float m_fallSeconds;
bool m_jumping;
List<Vector3> m_trail;
void UpdateJump()
{
float deltaTime = Time.time - m_lastUpdate;
if(deltaTime <= m_minUpdate)
return;
m_lastUpdate = Time.time;
if(m_trail == null)
m_trail = new List<Vector3>();
m_fallSeconds += deltaTime;
Vector3 curPos = transform.position;
Vector3 tarPos = m_target.position;
float curHeight = curPos.y;
float newHeight = curHeight;
newHeight += m_upwardsMomentum * deltaTime;
newHeight -= m_gravity * Mathf.Min(m_terminalVelocityTime, m_fallSeconds);
if(newHeight <= tarPos.y - 0.01f)
{
m_jumping = false;
newHeight = tarPos.y;
}
float newForward = curPos.x + (m_forwardMomentum * deltaTime);
Vector3 newPos = curPos;
newPos.y = newHeight;
newPos.x = newForward;
transform.position = newPos;
m_trail.Add(newPos);
}
void Update()
{
if(m_doJump)
{
m_jumping = true;
m_doJump = false;
m_lastUpdate = Time.time - m_minUpdate;
}
if(m_jumping)
UpdateJump();
Color startTrail = Color.green;
Color endTrail = Color.red;
if(m_trail != null)
{
for (int i = 0; i < m_trail.Count - 1; i++)
{
Color thisColor = Color.Lerp(startTrail, endTrail, (float)i/(float)m_trail.Count);
Debug.DrawLine(m_trail[i], m_trail[i + 1], thisColor);
}
}
}
}
Answer by Mischa · Dec 30, 2011 at 09:00 PM
Thanks for the answer above. Its not exactely the solution to my problem, but helpful! Maybe an image can explain a bit better what I'm looking for: The jump is no problem, the sinus floating is no problem, but how do I combine it? How do I do a transition from the jump to the sinus and vice versa?
Any ideas? Thanks for the answers so far!
Your answer
Follow this Question
Related Questions
CharacterController isGrounded toggles value 2 Answers
3rd person Character Control with kinect 0 Answers
When my player climb something, after a certain point it start to float 0 Answers
Character Controler Localising movement and gravity 1 Answer
Fly Mode 2D + Rigid Body vs CharacterController problems 0 Answers