1

I have a player that launches off a ramp. Everything works fine until that point.

When the player is off the ramp they are looking a little upwards so what I am trying to do is smoothly change the player (X, Y, Z) rotation; from the current one to a new one.

I always need y = -180

I always need z = 0

I need x = (MinAngle, MaxAngle) -> maybe these values (-30, 30)

Reason For X(MinAngle, MaxAngle)

I am trying to figure out how to rotate player on certain axis smoothly and stop at a certain angle so it can be applied here and possibly with player Buttons(Up/Down)

The transform.rotation Line Causes the Issue

rotCur = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;

transform.rotation = Quaternion.Lerp(Quaternion.Euler(30,-180,0), rotCur, Time.deltaTime * 5);
shane
  • 342
  • 1
  • 5
  • 28

1 Answers1

0

what exactly is your problem? you only said that it "does not work" - but what happens exactly?

have you tried this:

transform.rotation = Quaternion.Lerp(rotCur, Quaternion.Euler(30,-180,0), Time.deltaTime * 5);

the first and second parameters are swapped

i assume that rotCur means "current rotation" and in Quaternion.Lerp the first parameter is "from", the second is "to"

EDIT: have you tried this:

transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(30,-180,0), Time.deltaTime * 5);

EDIT 2: i know what your problem is!

you are using Time.deltaTime instead of Time.time (or any other timer)

deltaTime is never 0 or 1 - it is always the same, 0.2 for example

lerp interpolates between 0 and 1 and you have to increase the parameter from 0 to 1 to make it work

try this:

at the beginning, just once:

float interpolationProgress = 0.0f;
bool interpolating = true;

then every frame:

if(interpolating) {
    interpolationProgress += 0.1f * Time.deltaTime;
    transform.rotation = Quaternion.Lerp(from, to, interpolationProgress);
    if(interpolationProgress >= 1.0f) {
        interpolating = false;
    }
}
Jinjinov
  • 2,554
  • 4
  • 26
  • 45
  • Both mine line your line have the same effect. Also this is being called in update method. When the player leaves the ramp it has a jumpy up and down rotation happening but im trying to get it to be a smooth transition. – shane Apr 26 '18 at 10:14
  • @shane If you are worried about the smoothness, you can try Slerp and/or try a SmoothStep on your t variable inside of your lerp/slerp. They tend to give a more natural feel to movement. – Dtb49 Apr 26 '18 at 14:17
  • @Dtb49 If it is Slerp or Lerp I still get a jittery up and down rotation movement non-stop – shane Apr 26 '18 at 17:35
  • @shane it is jittery probably because you are suddenly changing the rotation in your lerp. You have: Quaternion.Lerp(to, from, t) where it should be Quaternion.Lerp(from, to, t) as the answer above suggests. – Dtb49 Apr 26 '18 at 19:05
  • @shane look at my EDIT 2, i think it has the right solution – Jinjinov Apr 26 '18 at 19:55