Jump to content

Changing the rigidbody.position of a KerbalEVA kerbal


Recommended Posts

Hi modding gurus.

I am trying to change the location of the active kerbal on EVA within the FlightScene.  Everything seems to working (identifying the kerbal, getting movement vectors, calculating new positions and doing animations). I am able to watch the kerbal move as intended.

The problem I am having occurs when I time warp. As soon as I change the game speed, my kerbal starts flickering - the animation plays, but the kerbal keeps moving back to the original position.

The code I am using is based on Marjin Steven's EVA Follower mod and looks like this:

Animation currentAnimation = null;
kerbal.GetComponentCached<Animation>(ref currentAnimation);

Rigidbody rigidbody = null;
kerbal.GetComponentCached<Rigidbody>(ref rigidbody);

if ((currentAnimation != null) && (rigidbody != null))
{
    var orientation = kerbal.part.vessel.transform.rotation;
    var deltaPosition = orientation * Vector3.forward.normalized * (TimeWarp.deltaTime * speed);

    currentAnimation.CrossFade(animation);
    rigidbody.interpolation = RigidbodyInterpolation.Extrapolate;

    var text = string.Format(
        "rigidbody is being moved from ({0:0.0000},{1:0.0000},{2:0.0000}) to ({3:0.0000},{4:0.0000},{5:0.0000}) at rate {6}",
        rigidbody.position.x,
        rigidbody.position.y,
        rigidbody.position.z,
        (rigidbody.position + deltaPosition).x,
        (rigidbody.position + deltaPosition).y,
        (rigidbody.position + deltaPosition).z,
        TimeWarp.CurrentRate
        );
    $"{text} velocity={rigidbody.velocity} constraints={rigidbody.constraints}".Debug();
    rigidbody.MovePosition(rigidbody.position + deltaPosition);
}

kerbal is the KerbalEVA object.
animation and speed are set elsewhere and are typically "wkC_forward" and KerbalEVA.walkSpeed;

When this is executed in the Update() method, the debug messages show the following:

[LOG 15:07:53.929] WalkAbout: rigidbody is being moved from (-1.3492,-0.5074,-2.0314) to (-1.4238,-0.5347,-2.1393) at rate 10 velocity=(0.0, 0.0, 0.0) constraints=None
[LOG 15:07:53.946] WalkAbout: rigidbody is being moved from (-1.4238,-0.5347,-2.1393) to (-1.4991,-0.5623,-2.2483) at rate 10 velocity=(-3.7, -1.4, -5.4) constraints=None
[LOG 15:07:53.962] WalkAbout: rigidbody is being moved from (-1.4238,-0.5347,-2.1393) to (-1.4973,-0.5617,-2.2457) at rate 10 velocity=(-3.7, -1.4, -5.4) constraints=None
[LOG 15:07:53.979] WalkAbout: rigidbody is being moved from (-1.3492,-0.5074,-2.0314) to (-1.4238,-0.5347,-2.1393) at rate 10 velocity=(0.0, 0.0, 0.0) constraints=None
[LOG 15:07:53.996] WalkAbout: rigidbody is being moved from (-1.4238,-0.5347,-2.1393) to (-1.4972,-0.5616,-2.2456) at rate 10 velocity=(-3.7, -1.4, -5.4) constraints=None
[LOG 15:07:54.013] WalkAbout: rigidbody is being moved from (-1.3492,-0.5074,-2.0314) to (-1.4241,-0.5348,-2.1397) at rate 10 velocity=(0.0, 0.0, 0.0) constraints=None

and when done in the FixedUpdate() method, it looks like this:
 

[LOG 14:42:42.548] WalkAbout: rigidbody is being moved from (-1.1389,-0.4248,-1.6948) to (-1.2278,-0.4575,-1.8238) at rate 10 velocity=(-4.4, -1.6, -6.4) constraints=None
[LOG 14:42:42.565] WalkAbout: rigidbody is being moved from (-1.1389,-0.4248,-1.6948) to (-1.2278,-0.4575,-1.8238) at rate 10 velocity=(-4.4, -1.6, -6.4) constraints=None
[LOG 14:42:42.581] WalkAbout: rigidbody is being moved from (-1.1389,-0.4248,-1.6948) to (-1.2278,-0.4575,-1.8238) at rate 10 velocity=(-4.4, -1.6, -6.4) constraints=None
[LOG 14:42:42.614] WalkAbout: rigidbody is being moved from (-1.1389,-0.4248,-1.6948) to (-1.2278,-0.4575,-1.8238) at rate 10 velocity=(-4.4, -1.6, -6.4) constraints=None

In both cases, the rigidbody is being reset back to an earlier position (after a few frames in Update() - and every time in FixedUpdate()).

I have searched to try and find out if there is some other object or property that I should be modifying instead of/in addition to the rigidbody, but to no avail.

Does anyone have any clue about this?

 

Link to comment
Share on other sites

1 hour ago, Diazo said:

Does this issue happen in both normal time-warp and physics time-warp?

INTERESTING!

Case 1:

  1. Kerbal is standing, time-warp is 0.
  2. Activate my code and kerbal starts walking.
  3. Increase the time-warp (either by clicking the arrows or pressing mod+.) - indicator arrows are green
  4. The kerbal starts jittering but does not move forward.

Case 2:

  1. Kerbal is standing, time-warp is 0.
  2. Increase the time-warp - indicator arrows are green.
  3. Activate my code
  4. The kerbal starts jittering but does not move forward.

Case 3:

  1. Kerbal is standing, time-warp is 0.
  2. Use W or shift-W to start the kerbal moving.
  3. Increase the time-warp - indicator arrows are yellow/red (and now only indicate 2x, 3x, 4x)
  4. Kerbal moves normally
  5. Activate my code - kerbal moves normally (except that pressing W or shift-W adds to the kerbal's speed, but that's okay)

So it seems that it does not occur in physics time-warp. (BTW - I really didn't know how to get into physics time-warp until I did this - so thanks for that).

Edited by Antipodes
Link to comment
Share on other sites

Possibly relevant From 1.2 notes: "If you are setting a vessel's position and/or changing its orbit, call Vessel.IgnoreGForces(framecount) on the vessel for the number of frames that g forces should be ignored (try 1, then 2 if that doesn't work, etc). "

The other thing would be that with rails warp, you probably need to be modifying orbital parameters somewhere. It's probably just putting it back where it was when it entered warp because physics doesn't apply

Edited by Crzyrndm
Link to comment
Share on other sites

9 hours ago, Crzyrndm said:

Possibly relevant From 1.2 notes: "If you are setting a vessel's position and/or changing its orbit, call Vessel.IgnoreGForces(framecount) on the vessel for the number of frames that g forces should be ignored (try 1, then 2 if that doesn't work, etc). "

Thanks - I didn't know about the notes.

9 hours ago, Crzyrndm said:

The other thing would be that with rails warp, you probably need to be modifying orbital parameters somewhere. It's probably just putting it back where it was when it entered warp because physics doesn't apply

That makes perfect sense! That would be the cause of my problem. Seeing as changing the orbital parameters without accounting for physics could lead to all kind of issues (popping up inside mountains, etc). it would be reasonable to restrict the movement of a kerbal to physics time-warp only.

I have tried this and it works.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...