Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make controller affect another object than the one controller is attached to
#1
Hello,

I'm currently in need of a way to make some SplineController affect another object than the object the controller is attached to... I started to create another class inheriting from SplineController and began to override ApplyTransformRotation, ApplyTransformPosition, ... in short every method that use transform and replacing it by objectToMove.transform. This would have been fine until I stumbled on the Refresh method from CurvyController which also use transform in its body. Unfortunatelay I cannot just copy that one since it uses protected/private fields.

I will thus have to modify the CurvyController directly to add this possibility. Right? Is there any caveat in doing that?

Thanks!
Reply
#2
You only need to override ApplyTransformRotation() and ApplyTransformPosition(), that should be sufficient. The only exception I see is when you want the controller to be able to switch splines like featured in the runner example.

In alternative, override UserAfterInit() and UserAfterUpdate() to set both position and rotation.

Edit: Examined the code and you're right, transform is used for getting rotation in Refresh(). So I consider adding GetTransformRotation() and GetTransformPosition() as new virtual methods to CurvyController.cs. If you want to modify CurvyController.cs by yourself, just add those two methods and your code should be fine after the next update. On the other side, must your GO holding the controller keep unchanged? Otherwise you could simply use UserAfterUpdate() and sync local transform to your other GO.

Also I'm curious what special purpose makes this neccessary.
Reply
#3
Actually I'm trying to use SplineController along other controllers implemented earlier for other type of movements. The position of the controller act as a kind of anchor for the movement themselves. This rules out the syncing solution you proposed as an alternative to changing the code I suppose. I seriously consider changing that in the future but meanwhile I try to keep things homegeneous.

I'd have to call the GetTransformPosition/Rotation on each update from another script, right? 

I have another proposition though. I might be missing something important here so my apologizes if this proposition is bad for some reasons. The transform property seems to be used for getting position/forward vector/up vector (I don't find rotation though), setting local or world position according to the Space property (others?). Won't it be easier to add a protected Transform field (and a corresponding property maybe) in CurvyController that defaults to the transform of the object on which the controller is attached to and use that instead of transform property in the existing methods? It would let us change that if we want without affecting the normal usage of the controller.

By the way, great support so far. Thank you.
Reply
#4
About the changes: I'll have a look into the code and see what's the best solution.

Regarding my syncing suggestion: Wouldn't it enough to have an empty GameObject just holding the controller, not being part of your movement hierarchy? But tbh I don't fully understand your GO setup. Do you have a screenshot or a sketch?
Reply
#5
Actually the GO setup is implementing a state machine through object activation/deactivation and is instantiated from game data. It does its job but has its flaws.

Enemy (higher level scripts): this root object never moves
- EnemyPrefab (Only MeshRenderer and colliders): the object to move
- ExitSpawnMovement (instantiated from a prefab with preconfigured Move controller script and a movement script): position of this object places (for example) a spline at the correct location
- ApproachMovement (idem)
- AttackMovement (idem)

Only one ***Movement object is active at a time.

I would have used the SplineController in place of our own move controller and put the spline on the same object... which is a bad idea right now and why I would have loved to be able to choose which object is moved by the controller. I'll certainly rewrite a controller for my special case but thought about the suggestion above which *might* have worked off the shelf.
Reply
#6
Had a look and your idea of having a virtual Transform property is the most easy and elegant, I think. Will be added to 2.0.5!
Reply
#7
Great! I'm looking forward to get my hand on it. I'm still working on my movement code and it changed a bit, as well as my GO setup. But I still need this feature. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Extending Curvy to make a Hallway editor. JacobSL 3 84 10-13-2021, 05:09 PM
Last Post: _Aka_
  Control of an object that uses a spline ricke 5 318 09-16-2021, 10:30 AM
Last Post: _Aka_
  Spline Controller Flickering blabz2007 6 403 09-03-2021, 09:29 AM
Last Post: _Aka_
  Make hard angle cll3m 5 1,069 07-01-2021, 04:06 PM
Last Post: _Aka_

Forum Jump: