Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Sprites facing the camera

Discussion in 'Scripting' started by MariuscoGames, Mar 17, 2021.

  1. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Hello, I am making a 3D game in unity 2019.4.22f1 and I would like to make a system like Baldi's Basics, in which the characters are 2D sprites but they always look towards the camera. Can I get some script to do that?
     
  2. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    Create a child of your object and attach a sprite renderer to it. Then add a script to the child with the following:
    Code (CSharp):
    1.  void Update() {
    2.         transform.LookAt(Camera.main.transform);
    3.     }
     
    MariuscoGames likes this.
  3. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Ok, it doesn't work for me ...
    Can you send the complete script?
    It does not tell me that there are errors, but the sprite does not always look at the camera, and I have followed the instructions to create a new object as a child of the sprite, I have put the render and the script, but it does not work
     
  4. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    and by the way, I do not know these influences, but I am not using the main camera, but a first person
     
  5. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Ok, I've already done it
     
  6. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    I have already worked well, but when the sprite rotates the camera also moves automatically ...
     
  7. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    I've already fixed it, I'll stop bothering you
     
  8. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    One last question ... How do I make it only turn sideways and not up and down?
     
  9. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    You should be able to use the 2nd parameter of the Look At function to achieve this. Specify the up direction:

    transform.LookAt(Camera.main.transform, Vector3.Up);

    Im on mobile so hopefully that is right.
     
  10. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    It's wrong, it says Vector3 didn't work with Up so I set it to lowercase to see if that was the error. The game was working, but it kept spinning up when the character jumped
     
  11. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Doesn't work...
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,002
    Let's slow down a bit here and identify what you're trying to do. This is basically called "sprite billboarding" and comes in various flavors:

    - aligned towards camera position
    - aligned towards camera +Z
    - aligned to something else?

    Now with that new search word, go check out "sprite billboarding" on Youtube tutorials and get a feel for what is involved. it will NOT just be a simple script. There will be VERY SPECIFIC hookup requirements, such as customizing it to the camera, the specific sprite, the anchor and pivot offset around which you want it to rotate, and perhaps many other things, including but not limited to the axes you are using for your game (X/Z vs X/Y for example).

    Screen Shot 2021-03-21 at 12.55.00 PM.png
     
  13. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    ok thanks, I couldn't search for that because I speak Spanish and didn't know what the script was called ...
     
  14. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Well, I have already learned how to do it and change the variables, since it seems that the script I found was old and I had to change a couple of things