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

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?

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.
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.
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?
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.
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!
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Set Volume to the Volume Controller at Runtime pako88 2 10 04-08-2024, 03:26 PM
Last Post: _Aka_
  Add noise to spline controller movement DekoGames 2 10 02-06-2024, 01:28 PM
Last Post: DekoGames
  Removing the objects behind an object alms94 6 6 12-06-2023, 09:31 PM
Last Post: _Aka_
  Getting object on spline Position when Spline has coordinates larger than 2000 velikizlivuk 5 11 09-05-2023, 01:01 PM
Last Post: velikizlivuk

Forum Jump: