- Home /
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;
}
}
}
}
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.
Your answer

Follow this Question
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