- Home /
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);
}
}
}
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
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!
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.
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.
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).
Yes, a Billboard type effect in Doom-like game. With some things Sprite based. Thanks!
Your answer
Follow this Question
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