playMaker

Author Topic: Clamped simple movement.  (Read 4348 times)

Red

  • Hero Member
  • *****
  • Posts: 563
Clamped simple movement.
« on: March 26, 2015, 04:45:31 PM »
EDIT: March 30, 2015. New version available... See linked comment.
http://hutonggames.com/playmakerforum/index.php?topic=9940.msg47314#msg47314

EDIT: April 2, 2015. New version available.
http://hutonggames.com/playmakerforum/index.php?topic=9940.msg47454#msg47454

So, I wanted a simpler way to clamp the movement of a player object than having to come up with a beastly system since clamping seems to be a very, very simple thing to do in code.

It's also my first actual action made so I am not 110% certain that it's perfect or what have you.

It similar to the translate action (which is what I picked apart to figure out how to do this one) but doesn't have relative or local space. It's a raw world-space translate. I guess I could add that in to another version but if you've needed a system like this then have fun.

It requires values in the boundaries (vertical and horizontal) since if it's all at 0 you won't move... Well, you might shift a tiny bit but the clamp will override that.
« Last Edit: April 02, 2015, 01:30:01 PM by Red »

Red

  • Hero Member
  • *****
  • Posts: 563
Re: Clamped simple movement.
« Reply #1 on: March 29, 2015, 09:56:19 PM »
Out of curiosity... I'm looking at the code now and I'm wondering.

Do I need to have this object use a rigid-body if it's only using the translate? Also, does this need to be a fixed-update (assuming the rigidbody isn't needed?)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Clamped simple movement.
« Reply #2 on: March 30, 2015, 03:41:35 AM »
Hi,

 thanks for your initiative. I would rename it so that it's clear its targeting a Rigidbody.

 Typically, you would set the velocity on fixedUpdate, but you would clamp on late update. But if you have no jitters when clamped, leave it like this. Double check when external forces are acting upon your RB. typically if a collider is pushing it, or if you add a force manually.

When you reach a version you are satisfied, I'll put it on the ecosystem.


 Bye,

 Jean

Red

  • Hero Member
  • *****
  • Posts: 563
Re: Clamped simple movement.
« Reply #3 on: March 30, 2015, 11:53:32 AM »
Okay, though I will certainly look for when to use update vs lateUpdate... What if I'm using the "transform.position" to manipulate it as opposed to velocity?

I did a test with a camera object set to follow with a basic vector3 lerping movement system... And I did notice some jerkiness in movement (when the camera is still I have also but it's very minimal to the point where I'm not certain if it's a frame-rate issue... I will do a test build for each case to see if that persists such that it's an issue I should be concerned about.)

EDIT: Did the testing... Tried something... The jitter I mention happens when I use the two actions in tandem (this is two actions with different functionality) either the OnUpdate or one uses OnUpdate and the other uses OnFixedUpdate. So I guess that suggests that using OnFixedUpdate addresses any jittering with these two actions interacting with one another.
« Last Edit: March 30, 2015, 12:28:20 PM by Red »

Red

  • Hero Member
  • *****
  • Posts: 563
Re: Clamped simple movement.
« Reply #4 on: March 30, 2015, 04:41:26 PM »
Sorry, brain-fart earlier. I completely missed that this is using the velocity to drive it so it does require rigidbody.

That said, I've managed to include a system to determine if the variables in the clamp-zone are set to "none" to set that value to the max float value. So now it can be given the functionality of only clamping it at a minimum or maximum. Case where this could be used... You don't want the player to go past the -1 mark but want them to have free reign on the other side.

Going to see about doing a bit of testing to ensure that the code works on all three axis-planes but if that passes I think I may have something to share that's improved over the above one.

Red

  • Hero Member
  • *****
  • Posts: 563
Re: Clamped simple movement.
« Reply #5 on: March 30, 2015, 05:53:52 PM »
(I'll be posting the new script here in this comment but I will link to this comment in the OP so as to keep the old version.)

Polished it up. If anyone wants to put it through it's paces, feel free to let me know if it is in need of any extra tweaking.

New features include:
  • Added a check OnAwake so if a variable for the clamping zone is set to "None" then it will input the maximum float value so it can be effectively used as a "Do not clamp this" feature. This way you can have it clamped on the positive but not the negative on each axis on the plane that it's working on (or vice-versa.)
  • Refactored it a bit from the previous version.

If you have criticism or suggestions on how to refine this further (or if you think there's a need for some more functionality) let me know. I didn't want to stuff TOO much in to this since this IS an action and not a complete project but I think I've managed to get a somewhat decent level of functionality for what it is intended to do.

Also, if you have some suggestions on how to refine it to be more elegant from a coding PoV, let me know. I'm still somewhat new to coding in C# so I suspect it may benefit from some refining... So I'm always open for crits to help this action improve as well as my own knowledge.
« Last Edit: March 30, 2015, 05:56:00 PM by Red »

Red

  • Hero Member
  • *****
  • Posts: 563
Re: Clamped simple movement.
« Reply #6 on: April 02, 2015, 01:29:18 PM »
Another change to this one... Changing some of the statements to conditionals to cut down on the code and make easier to read.