Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Charp · Sep 01, 2013 at 07:44 PM · 2drotation3dspritesvector

Making sprites just rotate and not tilt.

Was checking this script out in order to have my 2D sprites for plants, creatures, and text boxes always face my camera (I have heard there might be a way to do this in the terrain editor or with a shader, but that might be too advanced), however when my character controller looks up or down the sprites are tilting on the Z Axis. I am pretty new, and while I understand some javascript and have been watching tutorials, I am just unclear with this C# one. Thank you very much!

// CameraFacing.cs // original by Neil Carter (NCarter) // modified by Hayden Scott-Baron (Dock) - http://starfruitgames.com // allows specified orientation axis

using UnityEngine; using System.Collections;

public class CameraFacing : MonoBehaviour { Camera referenceCamera;

 public enum Axis {up, down, left, right, forward, back};
 public bool reverseFace = false; 
 public Axis axis = Axis.up; 
 
 // return a direction based upon chosen axis
 public Vector3 GetAxis (Axis refAxis)
 {
     switch (refAxis)
     {
         case Axis.down:
             return Vector3.down; 
         case Axis.forward:
             return Vector3.forward; 
         case Axis.back:
             return Vector3.back; 
         case Axis.left:
             return Vector3.left; 
         case Axis.right:
             return Vector3.right; 
     }
 
     // default is Vector3.up
     return Vector3.up;         
 }
 
 void  Awake ()
 {
     // if no camera referenced, grab the main camera
     if (!referenceCamera)
         referenceCamera = Camera.main; 
 }
 
 void  Update ()
 {
     // rotates the object relative to the camera
     Vector3 targetPos = transform.position + referenceCamera.transform.rotation * (reverseFace ? Vector3.forward : Vector3.back) ;
     Vector3 targetOrientation = referenceCamera.transform.rotation * GetAxis(axis);
     transform.LookAt (targetPos, targetOrientation);
 }

}

Before that script I was exploring this one, but for some vexing reason my 2D sprites were not oriented in the correct direction (I tried a few different things using empty game objects but I grew frustrated when I couldn't get it just right).

using System; using UnityEngine;

public class LookAtTarget : MonoBehaviour { public Transform target;

  void Update()
  {
       if(target != null)
       {
            transform.LookAt(target);
       }
  }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bubzy · Sep 01, 2013 at 07:54 PM

 using UnityEngine;
 using System.Collections;
 
 public class lookyHere : MonoBehaviour {
 
     // Use this for initialization
     public Transform target;
     float lookHeight = 0;
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     Vector3 tempPos = new Vector3(target.position.x,lookHeight,target.position.z);
     //or use the following line to make sure that the object only looks at its own level
 //Vector3 tempPos = new Vector3(target.position.x,transform.position.y,target.position.z);
         
     transform.LookAt(tempPos);
     }
 }

something like this may help. you can constrain where something looks at by creating a new vector 3 with the value set at 0 (or any other value). this way the look at wont bother changing that angle

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Charp · Sep 01, 2013 at 10:30 PM 0
Share

Thanks! This script is pretty close, however the sprites are still tilting up at me, as they are looking at my camera which is not level with the ground (where the sprites are). Tried editing the target.positions but that didn't really fix them, mostly just oriented them in another direction. Sorry if I'm not getting this 100%, thank you for your help though!

avatar image bubzy · Sep 02, 2013 at 05:55 AM 0
Share

edited, hope this will work for ya

avatar image
0

Answer by fherbst · Sep 01, 2013 at 07:48 PM

Create a new empty gameObject with the LookAtTarget-script (the bottom one), parent your sprite to it and rotate it such that "forward" is pointing along the blue arrow of the parent gameObject (in local mode). Then, the bottom script should just work fine. It's far easier to create this kind of hierarchy than to make a complicated script do the compensation.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Charp · Sep 01, 2013 at 09:29 PM 0
Share

Yeah, this is a tricky part. I have put my sprite in a gameObject and followed your suggestion, that does in fact work now (blue arrow orientation was a good hint), thanks. But my sprites are still tilting up to look at my main camera (or game object, or my invisible capsule.

avatar image
0

Answer by DESTRUKTORR · Sep 01, 2013 at 07:49 PM

Which part are you unclear about? Also, is this a billboard thing you want (as in 2D sprites facing a camera in a 3D world, like the original Doom games)? Or are you just trying to get all the sprites to face the camera so the perspective doesn't make them look not-flat?

If you're going for a 2D game, make sure you've set your camera to be orthographic. Chances are you don't need perspective, if you're doing a 2D game (unless you want the 2d Objects to exist in limited 3D space, rather than full 3D space).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Charp · Sep 01, 2013 at 09:23 PM 0
Share

Yes, a Billboard type effect in Doom-like game. With some things Sprite based. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Z Axis look at another object direction 1 Answer

2D object look at 3D object 0 Answers

3d Collision for 2d objects 1 Answer

Sprites always face the camera, Camera freely rotates 3 Answers

2D sprite that always faces the player? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges