- Home /
How to animate using accelerometer on Android?
Hello, everyone. This is my first post so I hope I'm in not posting it on the wrong place.
I'm trying to animate my Player while I'm using my Android Phone accelerometer but it doesnt switch animations. Even when it's still, the Idle animation doesnt play. What am I doing wrong?
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private Rigidbody2D rb;
private float xMv;
private float yMv;
private Vector2 direction;
private int scrWdt;
private int scrHgt;
private Vector2 screenSize;
private Animator anim;
[SerializeField] int mvntSpeed;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
scrHgt = Screen.height;
scrWdt = Screen.width;
screenSize = new Vector2(scrWdt, scrHgt);
}
void Update()
{
InputAccess();
}
void FixedUpdate()
{
rb.velocity = new Vector2(xMv, yMv) * mvntSpeed;
}
public void InputAccess()
{
xMv = Input.acceleration.x;
yMv = Input.acceleration.y;
//direction = new Vector2(xMv, yMv) * mvntSpeed;
//rb.AddForce(direction);
if(xMv > 0)
{
Debug.Log("Right");
anim.SetBool("Direita", true);
}
else if(xMv < 0)
{
Debug.Log("Left");
anim.SetBool("Left", true);
}
else if(yMv < 0)
{
Debug.Log("Down");
anim.SetBool("Down", true);
}
else if(yMv > 0)
{
Debug.Log("Up");
}
else
{
Debug.Log("Idle");
}
}
}
ur code is far from correct there is a lot of errors and unnecessary if also using booleans to set animations this way ins't good as u are only turning them on and never turning off and even if u were that wouldn't be the best approach
I can help improving ur code a lot if u want to send me ur discord or WhatsApp if u want (pela parte da "direita" acho q vc e br tb ne ? kk)
Thanks for replying, my discord is IAmPaullo#7147. I'll add you there.
(valeu pela disposição kk)
Your answer
Follow this Question
Related Questions
Animation in android game not working 2 Answers
Changing Animator State during run-time? 0 Answers
Can't get my Unity 2D animation to play properly 1 Answer
Why doesn't my UI cover fullscreen on android? 0 Answers
2D Animation does not start 1 Answer