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 /
avatar image
0
Question by vinhuman · Jul 25, 2014 at 07:35 AM · animationtouchcharacter controller

Setting up animation controller, need help with script for touch movement..


Edit: I found my solution. Given how I had things set up, it ended up being very easy to designate a bool to the animation controller as opposed to a float. I posted a bit more info in my comment below.


Let me preface this by saying I'm a total beginner. I am trying to rig up a 2d character so that the animation changes based on its horizontal movement speed by having left and right movement animations. For this I watched the Live Unity training video here: https://www.youtube.com/watch?v=Xnyb2f6Qqzg

I then wrote (copied) the code. My problem is I want my animations to changed based on clicking GUI textures which produce movement speed as opposed to the arrow keys which are used through the float move = Input.GetAxis("Horizontal") line. If anyone could explain how I could take the Unity live code and apply it to the touch based movement code I have, it would be greatly appreciated. I have a feeling I need to create a different anim.SetFloat, but I don't know exactly what to do. I will first put the Unity live script, and then follow it with the code I'm using for touch controls. Thanks for the help guys.

 using UnityEngine;
 using System.Collections;
 
 
 public class PlayerAnim : MonoBehaviour {
     
     public float maxSpeed =10.0f;
     bool facingRight = true;
 
     Animator anim;
 
     void Start()
     {
         anim = GetComponent<Animator>();
     }
     void FixedUpdate()
     {
         float move = Input.GetAxis ("Horizontal");
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
 
         anim.SetFloat ("Speed", Mathf.Abs(move));
 
         if (move > 0 && facingRight)
             Flip ();
         else if (move < 0 && !facingRight)
             Flip ();
     }
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof (Rigidbody2D))]
 [RequireComponent(typeof(BoxCollider2D))]
 public class TouchControls : MonoBehaviour {
     
     // GUI textures
     public GUITexture guiLeft;
     public GUITexture guiRight;
     public GUITexture guiJump;
     
     // Movement variables
     public float moveSpeed = 5f;
     public float jumpForce = 50f;
     public float maxJumpVelocity = 2f;
     
     
     // Movement flags
     private bool moveLeft, moveRight, doJump = false;
     
     //TESTING ANIMATION
     Animator anim;
 
     void Start()
     {
         anim = GetComponent<Animator>();
     }
 
 
     // Update is called once per fram
     void Update () {
         
         // Check to see if the screen is being touched
         if (Input.touchCount > 0)
         {
             // Get the touch info
             Touch t = Input.GetTouch(0);
             
             // Did the touch action just begin?
             if (t.phase == TouchPhase.Began)
             {
                 // Are we touching the left arrow?
                 if (guiLeft.HitTest(t.position, Camera.main))
                 {
                     Debug.Log("Touching Left Control");
                     moveLeft = true;
                 }
                 
                 // Are we touching the right arrow?
                 if (guiRight.HitTest(t.position, Camera.main))
                 {
                     Debug.Log("Touching Right Control");
                     moveRight = true;
                 }
                 
                 // Are we touching the jump button?
                 if (guiJump.HitTest(t.position, Camera.main))
                 {
                     Debug.Log("Touching Jump Control");
                     doJump = true;
                 }
             }
             
             // Did the touch end?
             if (t.phase == TouchPhase.Ended)
             {
                 // Stop all movement
                 doJump = moveLeft = moveRight = false;
                 rigidbody2D.velocity = Vector2.zero;
             }
         }
         
         // Is the left mouse button down?
         if (Input.GetMouseButtonDown(0))
         {
             // Are we clicking the left arrow?
             if (guiLeft.HitTest(Input.mousePosition, Camera.main))
             {
                 Debug.Log("Touching Left Control");
                 moveLeft = true;
             }
             
             // Are we clicking the right arrow?
             if (guiRight.HitTest(Input.mousePosition, Camera.main))
             {
                 Debug.Log("Touching Right Control");
                 moveRight = true;
             }
             
             // Are we clicking the jump button?
             if (guiJump.HitTest(Input.mousePosition, Camera.main))
             {
                 Debug.Log("Touching Jump Control");
                 doJump = true;
             }
         }
         
         if (Input.GetMouseButtonUp(0))
         {
             // Stop all movement on left mouse button up
             doJump = moveLeft = moveRight = false;
             rigidbody2D.velocity = Vector2.zero;
         }
     }
     
     void FixedUpdate()
     {
         // Set velocity based on our movement flags.
         if (moveLeft)
         {
             rigidbody2D.velocity = -Vector2.right * moveSpeed;
         }
         
         if (moveRight)
         {
             rigidbody2D.velocity = Vector2.right * moveSpeed;
         }
         
         if (doJump)
         {
             // If we have not reached the maximum jump velocity, keep applying force.
             if (rigidbody2D.velocity.y < maxJumpVelocity)
             {
                 rigidbody2D.AddForce(Vector2.up * jumpForce);
             } else {
                 // Otherwise stop jumping
                 doJump = false;
             }
         }
     }
 }


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 vinhuman · Jul 26, 2014 at 03:45 AM 0
Share

Just thought I'd post the answer to my problem. I was correct in guessing it might be an easy fix, but it took me a long time to discover that fix. I needed to set a bool ins$$anonymous$$d of a float for my controller. I used moveLeft and moveRight which I already had created and then rigged the animation so when moveLeft and moveRight were true/false the character moves accordingly. Beginner problems haha.

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple animation with single button 1 Answer

Character controller problem 0 Answers

Changing Bones positions and rotation in OnAnimatorMove not working. 0 Answers

Press GUI Button and make it play animation on Android 1 Answer

Animation Problem . 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