Search Unity

Rotation off when trying to make sprites follow a dolly track

Discussion in 'Cinemachine' started by lucvw_1975, May 24, 2020.

  1. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Hi,

    I want to use Cinemachine to make my sprites follow a track (it's for a space shooter). So I tried adding a Dolly Track and then I created my sprites and attached the Cinemachine Dolly Cart script. The problem is that the rotation of the sprite is completely off as soon as I attach the script. When I turn it off, rotation is like it should. When I turn it on, rotation is in a way I can't even see the sprite... :/

    I tried everything I could think of, even make some changes in the Dolly script, but to no avail.

    I've uploaded 2 screenshots. The first is how the rotation of the enemy should be. The second one is after activating the Dolly Cart script. You can see that the sprite is rotated along it's y-axis and I'm unable to change that rotation in the inspector. Once the script is activated, the transform controls are locked on the object.
    before_dolly_track.jpg after_dolly_track.jpg
    Can someone help please, I'm going nuts atm...

    Thanks !
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hi,
    You could put this simple script on your sprite:
    Code (CSharp):
    1. public class FollowDollyCart : MonoBehaviour
    2. {
    3.     public Transform dollyCart;
    4.     void Update()
    5.     {
    6.         transform.position = dollyCart.position;
    7.     }
    8. }
    Instead of attaching the sprite as a child of the DollyCart.
     
  3. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Hmm, that doesn't seem to work. My sprite just jumps to (0,0,0) and it stays there (doesn't move)...

    [UPDATE] I think I did something wrong. I attached the script to the Track and not the Cart, but the rotation is still a problem.
     
    Last edited: May 26, 2020
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Could you send a few screenshots of your setup? Screenshots of your components and game object hierarchy.
    Or you can send me your project and I'll have a look.
     
  5. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    It's the first time I send a project to someone, so hope I did it right ;)
     

    Attached Files:

  6. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    To make the dollyCart move through the dollyTrack you need to set the speed on the dollyCart. After that the simple script will work.
    This way, your rotation won't be locked to the dollyCart's rotation.
     

    Attached Files:

  7. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Wasn't this set to a value other than zero ? In my project speed = 5. No changes in rotation problem...
     
  8. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    I don't have a problem with the rotation when using my script. I can change the rotation on the sprite to whatever value I'd like.

    Make sure to set dollyCart in the script.
     
  9. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Okay, just to make clear. I wonder if we are talking about the same thing. I can rotate my sprite also, but once I press play, the rotation changes to an angle that's wrong... The whole ship should rotate so the nose of the sprite is following the curves in the dolly track.
     
  10. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Ah. In that case, you just want to child the sprite to the dolly cart (see the image). Then set the initial rotation on the sprite so it looks forward on your dolly track.

    Note, that in this case, you do not need the script I provided.
     

    Attached Files:

  11. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Just to be sure we're talking about the same... I can rotate my sprite also, but after I press play it is locked and being rotated in the wrong way. I want the ship and its "nost"
    Doesn't work either. I'm getting tired of this :/. Could you send me a working project with just a dolly track/cart and sprite (preferably with some curves in it) so I can see if this works and what I'm doing wrong ? Thanks !
     
  12. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    You will need to import Cinemachine to make it work.
     

    Attached Files:

    lucvw_1975 likes this.
  13. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Okay, this actually works ! But I don't understand why...The enemy sprite doesn't even have a component attached that links it to the dolly track/cart and it's also not a child of the dolly track/cart. So how on earth is this working ?

    [UPDATE] Oops, sorry, now I see that the Enemy's parent is the DollyCart :) But still I don't know completely how this can work, because I tried this setup multiple times if I remember right... And I wasn't able to rotate the sprite once it was a child of the Dolly Cart. Maybe I made it a child of the Dolly Track. I don't know anymore, I tried a lot of different approaches. Glad to see it work now. There is however another problem. If I want multiple sprites in a row follow the first one, then they all make a turn if the first one does and so they aren't following the path anymore...

    Okay, I can see that, when I attach a second sprite, it follows the transformation gizmo of the first sprite. So I thought, I make a new Cart for the second enemy and so on. But the position transform stays the position transform of the first cart and I'm unable to edit that.
     
    Last edited: May 27, 2020
  14. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    To achieve this, create more DollyCarts on the track. Add one enemy sprite per DollyCart.

    To position each cart, change the Cinemachine Dolly Cart script's Position parameter. Have a look at the attached images.
     

    Attached Files:

    Last edited: May 27, 2020
  15. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Nope, this isn't working either... I guess I'm just stupid... If it's not too much asked, could you try to make this behavior and send it to me please ?
     
  16. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    I hope the attached package helps. Let us know what was confusing, so we can improve our UX :)
     

    Attached Files:

  17. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    This is working great ! But I still don't understand why it didn't work on my side. I also added extra Dolly Carts and one enemy per cart. The only thing that's different from my solution is that you gave each dolly cart different position and rotation values. But how do I know what values to choose ?
     
  18. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    I did not change the DollyCart's Transform (position, rotation) directly; the Transform values are calculated based on the Position parameter of the Cinemachine Dolly Cart Script. So I only changed the Cinemachine Dolly Cart's Position value.

    You can see the result real-time in the Scene view.
     
  19. lucvw_1975

    lucvw_1975

    Joined:
    Dec 7, 2014
    Posts:
    20
    Ah okay, I see it now. Well, finally have a working setup now. Thanks for the time taken to help me out with this. As far as improving the UX: I don't know what could be altered to make it more intuitive. I guess it's okay like it is, just a little difficult to setup for a situation like mine. But once you know how, it's actually very simple :) So thanks again ! I now can finally start animating my enemies (although I also have some scripts based on sine and cosine to make some "simpler" movements, maybe I'll even try my hands at a Lissajous curve).
     
    gaborkb likes this.