- Home /
sprite offset in game but not in scene
I have a small problem with my current project and hope you guys can help me out.
I'm making a small 2D game (and i mean 2D -> 2d mode, 2d colliders, 2d physics, sprites, etc.).
For some reason it seems like the arm of my mage character gets offset over multiple walking cycles (not 1, so not a simple mistake in the animation). Actually it's not just the arm, but that one is most noticeable.
The strangest part is, that the change is only visible in the game view during play, it seems fine in the scene view.
Also, the (x,y,z) coordinates seem to be as they should be (you might detect a slight offset in the "arm_offset_problem.jpg" but that is caused by the breathing idle animation).
I checked that there is no offset between the start and end of the walking animation(though that shouldn't have any impact anyway since it loops).
I restarted unity and build the project, thinking it might be a play mode problem but it's the same in the build game.
There is only one script that impacts the movement at all. It's placed on the "Chibi_Mage" GameObject and lets me move around the mage (simple transform movement, don't need physics yet; changes apply to the parent GameObject "Chibi_Mage" so it shouldn't affect the different child sprites at all).
Below you will find the movement script and a few pictures to get a better feeling for the setup.
I'm using Unity version 4.5.2f1.
using UnityEngine;
using System.Collections;
public class ChibiBattleMoves : MonoBehaviour {
public float walkspeed = 10f;
public bool turnblock = false;
// private Vector2 moveDir = new Vector2(0,0);
private Animator myAnimator;
// Use this for initialization
void Start () {
myAnimator = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Vector3 move = new Vector3(Input.GetAxis("Horizontal"),
Input.GetAxis("Vertical"), 0);
gameObject.transform.position += move * walkspeed * Time.deltaTime;
myAnimator.SetFloat("AnimWalkSpeed", move.magnitude);
if(move.x > 0 &&
turnblock == false)
gameObject.transform.localScale = new Vector3(
Mathf.Abs(gameObject.transform.localScale.x),
gameObject.transform.localScale.y,1);
if(move.x < 0 &&
gameObject.transform.localScale.x > 0 &&
turnblock == false)
gameObject.transform.localScale = new Vector3(
gameObject.transform.localScale.x * -1,
gameObject.transform.localScale.y,1);
/*
if(gameObject.transform.rotation.y == 0 && move.x > 0)
gameObject.transform.localRotation = Quaternion.Euler (0,180,0);
if(gameObject.transform.rotation.y == 180 && move.x < 0)
gameObject.transform.localRotation = Quaternion.Euler (0,180,0);
*/
}
}
Off-hand, I'd guess you're using a perspective camera when you really should be using an orthographic camera.
That was it! Thank you very much ^^'. I didn't keep in $$anonymous$$d that the 2D mode button only applies to the scene viewport. How do I mark this as answered?
Answer by Pyrian · Sep 03, 2014 at 06:23 PM
Off-hand, I'd guess you're using a perspective camera when you really should be using an orthographic camera.
I just spent an hour trying to figure out what was causing this. You sir are a life-saver.
this was part of my problem too. The other thing to check is your z axis. Despite my being in 2D the whole time, my parent sprite had somehow gotten moved 0.7 on the z. So annoying. Thanks for the help.
P.S. If you're having this problem and its not your camera or your z axis, you could try adding your sprites to a sorting group or changing the "Order in layer" in the sprite renderer > additional settings
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Damage gets applied multiple times per frame 0 Answers
Damn, HOW CAN I KNOW, IF MY 2d ANIMATION SPRITE IS FINISHED??!! 0 Answers
SpriteManager 2 1 Answer
Best way to do animate soldier? 1 Answer