Search Unity

Rotate Sprite Independently of Character Controller

Discussion in 'Editor & General Support' started by VX967, Jun 16, 2014.

  1. VX967

    VX967

    Joined:
    Jun 11, 2014
    Posts:
    6
    I have a unique situation: for anyone familiar with Paper Mario, I'm working with a 3D game that uses 2D character sprites. So I'm in 3D mode with a Character Controller that has no Mesh Renderer, but a Sprite Renderer instead.

    My question is, can I rotate the sprite independently of the collision capsule? I'd like to grab the Sprite in C# using something like this (pseudo code):

    Code (CSharp):
    1. public class AnimationManager : MonoBehaviour {
    2.  
    3.     SpriteRenderer sprite;
    4.  
    5.     void Start() {
    6.         sprite = GetComponent<SpriteRenderer>();
    7.     }
    8.  
    9.     void Update() {
    10.         sprite.transform.rotate(//rotate y based on direction of velocity);
    11.     }
    First of all, I know I can't rotate the Sprite Renderer itself... but is there a way to grab the Sprite from the Renderer and rotate that?

    NOTE: Rotating the entire character- and collision capsule- doesn't seem to be available as a viable option. It messes up any movement, since .Move operates on a local scale. I'd rather rotate the sprite itself while leaving the collision capsule alone.
     
  2. destructivArts

    destructivArts

    Joined:
    Jan 30, 2013
    Posts:
    29
    This is the first solution that popped into my head:

    Create these objects:
    GameObject #1 with components player controller script/character controller
    GameObject #2 with sprite renderer and animation manager

    Make GO#2 the child of GO#1 and in your scripts just expose publicly any functions you need access to from the other scripts.
     
  3. VX967

    VX967

    Joined:
    Jun 11, 2014
    Posts:
    6
    Thanks for that; it's a simple suggestion but it definitely helps clean things up a bit.

    However, now I'm running into another problem. I'm trying to rotate the sprite a set amount of degrees over a set period of time (for example, rotate 180 degrees over 1 second when switching directions), but for the life of me I can't figure out how. I've been researching the problem all day, and from what I can tell, no one has really offered a sure way to do it. Does someone know how to do this?
     
  4. destructivArts

    destructivArts

    Joined:
    Jan 30, 2013
    Posts:
    29
    If you know the length of time over which you want to rotate, the axis on which you want the rotation to take place and the amount you want to rotate the object, it should be pretty easy.

    float totalRotationTime = 1.0f;
    Vector3 totalRotationDegrees = new Vector3(0,180,0);

    Vector3 rotationThisFrame = totalRotationDegrees * (float)(totalRotaionTime/Time.deltaTime);
    transform.Rotate(rotationThisFrame);
     
  5. VX967

    VX967

    Joined:
    Jun 11, 2014
    Posts:
    6
    I've tried a few methods just using transform.Rotate, and they all end up rotating endlessly. I wanted to control the rotation through angle instead of total time, so I've implemented the Slerp function, which is working okay for the most part.

    However, I'm having a bit of trouble with the execution. Is it supposed to run in its own loop or something? Because it seems like it rotates for one frame, and then stops. For now I've set it to rotate the sprite the full way pretty much instantly, but I'd like to figure out how to have a smooth rotation over time using Slerp.