Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make 2D sprite face movement Direction

Discussion in 'Editor & General Support' started by DarkOdeus, Jun 27, 2014.

  1. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    I am starting a new project with 2D and I got the character to move using this script. This is a top down script so it should face up down left and right

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     void FixedUpdate ()
    9.     {
    10.         float moveHorizontal = Input.GetAxis ("Horizontal");
    11.         float moveVertical = Input.GetAxis ("Vertical");
    12.  
    13.         Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    14.         rigidbody.velocity = movement * speed;
    15. }
    16. }
    This moves the character along the y and the x axis but does not rotate them. What is the easiest way to make the character look where they are walking

    Push D move right face right
    Push A move left face left etc

    It seems to be worth noting my artist is making sprites for each direction. How would I code this in for the movement animation as well?
     
  2. Reaner

    Reaner

    Joined:
    Nov 19, 2013
    Posts:
    1
    Try:

    transform.up = rigidbody2D.velocity.normalized;

    every frame.

    EDIT: If you have a sprite for each direction you can use the animation system to have different states for each direction. Use your horizontal and vertical input to determine which direction the player if moving and enter the relevant animation state.
     
  3. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    Adding it gave me a complier error. It's not taking the transform command
     
  4. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    DarkOdeus: It looks like in your script you're using a rigidbody, not a rigidbody2D as Reaner's script uses. That might be the error, so give that a try?


    You should probably be using a rigidbody2D for your 2d sprites/characters anyway if you're making a 3d game as far as I know. The only 3d elements I use in my 2d platformer are 3d colliders for particle collision as when I started making it there was no 2d particle collision system.
     
  5. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    Is the conversion to rigidbody2d simple. When I wrote the code I attempted to use a rigidbody2d and it would not take.
     
  6. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     void FixedUpdate ()
    9.     {
    10.         float moveHorizontal = Input.GetAxis ("Horizontal");
    11.         float moveVertical = Input.GetAxis ("Vertical");
    12.  
    13.         Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    14.         rigidbody2d.velocity = movement * speed;
    15.    
    16. }
    17. }
    This is the updated code with a rigidbody2d. The compiler error I get is saying that rigidbody2d does not exist in this context. How would I fix this?
     
  7. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    The GameObject you're attaching the script to must have a RigidBody2D component, not a RigidBody.

    Also, it has to be rigidbody2D with a capital D.

    As far as converting it from RigidBody to RigidBody3D you're fine in the code, nothing you need to change at all.

    Edit: I just tested your script and it works fine with the capital D.

     
    Last edited: Jun 28, 2014
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936
    and that should be big D:
    rigidbody2D.velocity = movement * speed;
     
  9. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    Got it to work with the Big D. Such a newbie mistake :p
     
  10. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    The character now moves but I have a slight issue. I applied the line transform.up = rigidbody2D.velocity.normalized; to my script and since the camera is bound to the character it rotates so the character is always moving up. How do I make it so it does not rotate the camera. Is there a setting for the camera to lock rotation during gameplay
     
  11. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    Two issues. One. I have the camera parented to the player so the player rotation works but it rotates the camera as well. How do I stop this? Also when the player stands idle the return to the start position (facing towards the top of the screen for example) is there any way to make it so the character does not idle like that IE. Stay facing the direction they were even when stopped
     
  12. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    You'd be much better off not parenting the camera to the player, instead have the camera it's own thing with a script to follow the player.

    Code (JavaScript):
    1. var player:Transform;
    2. var xOffset:float;
    3. var yOffset:float;
    4.  
    5. function Awake()
    6. {
    7.     player=GameObject.Find("Player").transform;
    8. }
    9.  
    10. function Update()
    11. {
    12.     transform.position.x=player.position.x+xOffset;
    13.     transform.position.y=player.position.y+yOffset;
    14. }
    Then in the inspector you can set the xOffset and yOffset if you don't want the camera to be dead center on the player; otherwise just leave them at 0. Also change ("Player") to whatever you're controlling.
     
  13. DarkOdeus

    DarkOdeus

    Joined:
    Jul 2, 2013
    Posts:
    9
    Anyway you have a C# conversion of this. It's currently my primary language (learning) and I want to keep all the code of my game the same language
     
  14. Brutang

    Brutang

    Joined:
    Nov 1, 2013
    Posts:
    23
    I primarily use JavaScript so there might be a more efficient way of doing this, but I tested it and it works.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. class CameraFollow: MonoBehaviour {
    6.  
    7.    Transform player;
    8.    public float xOffset=0;
    9.    public float yOffset=0;
    10.    public float zOffset=0;
    11.    Vector3 playerPosition;
    12.  
    13.    void Start()
    14.    {
    15.      player = GameObject.Find ("Player").transform;
    16.    }
    17.  
    18.    void  Update ()
    19.    {
    20.      playerPosition.x = player.position.x + xOffset;
    21.      playerPosition.y = player.position.y + yOffset;
    22.      playerPosition.z = player.position.y + zOffset;
    23.      transform.position = playerPosition;
    24.    }
    25.  
    26. }
    27.  
     
    Last edited: Jul 1, 2014