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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Davenger · Jun 13, 2014 at 07:41 PM · 2dmecanim

applying animations on a top-down 2d game

i've recently started to learn coding alongside this project to make a top-down rpg,and i am having some trouble assigning my walking and idling animations to my character. first off i have used the following code to make the movement so i can make the movement grid based and disable diagonals and disable moving to a collider

 using System.Collections;
 using UnityEngine;
 
 class Movementt : MonoBehaviour {
     public float moveSpeed = 3f;
     public float runSpeed = 6f;
 
     private float gridSize = 1f;
     private bool allowDiagonals = false;
     private bool correctDiagonalSpeed = true;
     private Vector2 input;
     private bool isMoving = false;
     private Vector2 startPosition;
     private Vector2 endPosition;
     private float t;
     private float factor;
 
 
     public void Start ()
     {
 
         }
 
 
 
 
     public void FixedUpdate() {
         if (!isMoving) {
             input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
             if (!allowDiagonals) {
                 if (Mathf.Abs(input.x) > Mathf.Abs(input.y)) {
                     input.y = 0;
                 } else {
                     input.x = 0;
                 }
             }
 
             
             if (input != Vector2.zero) {
                 StartCoroutine(move(transform));
             }
         }
     }
     
     public IEnumerator move(Transform transform) {
         isMoving = true;
 
         t = 0;
         
 
             
         
         if(allowDiagonals && correctDiagonalSpeed && input.x != 0 && input.y != 0) {
             factor = 0.7071f;
         } else {
             factor = 1f;
         }
         startPosition = rigidbody2D.transform.position;
         endPosition = new Vector2 (startPosition.x + System.Math.Sign (input.x) * gridSize,
                                    startPosition.y + System.Math.Sign (input.y) * gridSize);
         RaycastHit2D right = Physics2D.Raycast (startPosition, Vector2.right, 1f);
         RaycastHit2D left = Physics2D.Raycast (startPosition, -Vector2.right, 1f);
         RaycastHit2D up = Physics2D.Raycast (startPosition, Vector2.up, 1f);
         RaycastHit2D down = Physics2D.Raycast (startPosition, -Vector2.up, 1f);
 
 
         if (right.collider != null && endPosition.x > startPosition.x) {
                         endPosition = startPosition;
         }
         if (left.collider != null && endPosition.x < startPosition.x) {
             endPosition = startPosition;
         }
         if (up.collider != null && endPosition.y > startPosition.y) {
             endPosition = startPosition;
         }
         if (down.collider != null && endPosition.y < startPosition.y) {
             endPosition = startPosition;
         }
 
 
 
 
 
         while (t < 1f)
         {
                 if (Input.GetKey (KeyCode.LeftShift)) {
             t += Time.deltaTime * (runSpeed / gridSize) * factor;
                         }
         else {
                 t += Time.deltaTime * (moveSpeed/gridSize) * factor;
             }
             transform.position = Vector2.Lerp(startPosition, endPosition, t);
             yield return null;
         }
         
         isMoving = false;
         yield return 0;
     }
 
 }

but since my characters is 2 units long and i want it to raycast from the feet i added the character as a child entity with y=1,and added the following code to try and animate it

 using UnityEngine;
 using System.Collections;
 
 public class playeranimation : MonoBehaviour {
     Animator anim;
     private Vector2 input;
     // Use this for initialization
     private float compareX;
     private float compareY;
     void Start () {
         anim = GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void fixedUpdate () {
         input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
         if (Mathf.Abs (input.x) > Mathf.Abs (input.y)) {
                         input.y = 0;
                 } else {
                         input.x = 0;
                 }
         compareX = input.x;
         compareY = input.y;
         if (compareX > 0) {
                         if (Input.GetKey (KeyCode.LeftShift)) {
                                 anim.SetFloat ("MoveDirection", 2);
                         } else {
                                 anim.SetFloat ("MoveDirection", 1);
                         }
 
                         anim.SetFloat ("IdleDirection", 2f);
                 }
         if (compareX < 0) {
             if (Input.GetKey (KeyCode.LeftShift)) {
                 anim.SetFloat ("MoveDirection", -2);
             } else {
                 anim.SetFloat ("MoveDirection", -1);
             }
             
             anim.SetFloat ("IdleDirection", 3f);
         }
         if (compareY > 0) {
             if (Input.GetKey (KeyCode.LeftShift)) {
                 anim.SetFloat ("MoveDirection", 4);
             } else {
                 anim.SetFloat ("MoveDirection", 3);
             }
             
             anim.SetFloat ("IdleDirection", 0f);
         }
         if (compareY < 0) {
             if (Input.GetKey (KeyCode.LeftShift)) {
                 anim.SetFloat ("MoveDirection", -4);
             } else {
                 anim.SetFloat ("MoveDirection", -3);
             }
             
             anim.SetFloat ("IdleDirection", 1f);
         }
         if (input == Vector2.zero) {
                         anim.SetFloat ("Movedirection", 0);
                 }
 
 
 
 
 
 
     }
 }

and although i set these values to the animations none play whatsoever

Comment
Add comment · Show 1
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 Davenger · Jun 13, 2014 at 10:55 PM 0
Share

updated the question with a bit more details

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by cyberlarry · Jun 17, 2014 at 11:34 PM

Try this, more easy code and later do a tutorial about blend tree. It works for me. You will move in 8 directions.

using UnityEngine; using System.Collections;

public class PlayerScript : MonoBehaviour {

     private Animator anim;
 public Vector2 speed = new Vector2(4, 4);

 private Vector2 movement;

         void Start ()
     {
         anim = GetComponent<Animator>();
     }
     
     void FixedUpdate()
     {
     {
         rigidbody2D.velocity = movement;
     }
                     
             float lastInputX = Input.GetAxis ("Horizontal");
             float lastInputY = Input.GetAxis ("Vertical");
         
                     if (lastInputX != 0 || lastInputY != 0) {
                     anim.SetBool ("walking", true);
             
                         
                     if (lastInputX > 0) {
                             anim.SetFloat ("LastMoveX", 1f);
                     } else if (lastInputX < 0) {
                             anim.SetFloat ("LastMoveX", -1f);
                     } else {
                             anim.SetFloat ("LastMoveX", 0f);
                     }
             
                     if (lastInputY > 0) {
                             anim.SetFloat ("LastMoveY", 1f);
                     } else if (lastInputY < 0) {
                             anim.SetFloat ("LastMoveY", -1f);
                     } else {
                             anim.SetFloat ("LastMoveY", 0f);
                     }
             

             } else {
                     anim.SetBool ("walking", false);
             }
     }

     void Update ()

 {
 
     float inputX = Input.GetAxis("Horizontal");
     float inputY = Input.GetAxis("Vertical");
     
 
     movement = new Vector2(
         speed.x * inputX,
         speed.y * inputY);

         
         anim.SetFloat ("SpeedX", inputX);
         anim.SetFloat ("SpeedY", inputY);
         
                 }
 }

Later use the blend tree DOUBLE CLICK IN ANIMATOR STATE, is very easy. Look this for the blend tree.

https://www.youtube.com/watch?v=7URRg8J6mz8

I hope that this help you. Sorry for my english.

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 Creative Inventory · Dec 23, 2015 at 12:27 PM 0
Share

Hey, sorry but would you know how to do the same thing but ins$$anonymous$$d of using the key arrows to move I want to use click to mouse

avatar image
0

Answer by Angoo1 · Apr 02, 2017 at 09:56 AM

I want to move my car exactly like an android game 2 cars. any script ? please help

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Edit Animator animationClips ? Editor 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make basic AI in a 2d game? 4 Answers

Getting a list of mecanim states in Animator 9 Answers

Limiting Player Movement in 2D game? 0 Answers


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