Hi guys,
i’m just finishing a new project and i got an issue that is driving me mad…
I got my character moved on the X axis using the accelerometer, it works great quite basic script.
I need to play a “Left” animation and a “Right” animation according to the actual direction of the character (its a skateboarding game, the character moves only on the X axis and the environment moves to simulate the run.
Any solution?
Iguana_02
EDIT: Solved on my own (as usual ehehe)
Just needed to check if was >0 or <0.
iguana_02:
Hi guys,
i’m just finishing a new project and i got an issue that is driving me mad…
I got my character moved on the X axis using the accelerometer, it works great quite basic script.
I need to play a “Left” animation and a “Right” animation according to the actual direction of the character (its a skateboarding game, the character moves only on the X axis and the environment moves to simulate the run.
Any solution?
Iguana_02
EDIT: Solved on my own (as usual ehehe)
Just needed to check if was >0 or <0.
How did you move you character left and right based on accelerometer?
Hi Imran Khalil here it is:
using UnityEngine;
using System.Collections;
public class tiltFunction : MonoBehaviour {
public float speed = 10.0F;
public float minPos = 15f;
public float maxPos = -12f;
private Vector3 dir;
private bool isRunning = false;
void Start(){
}
void Update() {
dir = Vector3.zero;
dir.x = -Input.acceleration.x;
dir.z = 0;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
Debug.Log(dir.x);
if(Input.touchCount==0&dir.x>0&isRunning==false){
StartCoroutine (leftAnimation());
}
else if(Input.touchCount==0&dir.x<0&isRunning==false){
StartCoroutine (rightAnimation());
}
//This limits the movement for the Left
if(transform.localPosition.x>minPos){
transform.localPosition = new Vector3 (minPos,transform.localPosition.y,transform.localPosition.z);
}
//This limits the movement for the Right
else if(transform.localPosition.x<maxPos){
transform.localPosition = new Vector3 (maxPos,transform.localPosition.y,transform.localPosition.z);
}
}
IEnumerator leftAnimation(){
isRunning=true;
animation["left"].layer = 5;
animation["left"].wrapMode = WrapMode.Once;
animation["left"].speed = 2;
animation.CrossFade("left",0.3f);
yield return new WaitForSeconds(animation["left"].clip.length);
isRunning=false;
}
IEnumerator rightAnimation(){
isRunning=true;
animation["right"].layer = 5;
animation["right"].wrapMode = WrapMode.Once;
animation["right"].speed = 2;
animation.CrossFade("right", 0.3f);
yield return new WaitForSeconds(animation["right"].clip.length);
isRunning=false;
}
}
eciosf
August 8, 2013, 5:51pm
4
iguana_02:
Hi Imran Khalil here it is:
using UnityEngine;
using System.Collections;
public class tiltFunction : MonoBehaviour {
public float speed = 10.0F;
public float minPos = 15f;
public float maxPos = -12f;
private Vector3 dir;
private bool isRunning = false;
void Start(){
}
void Update() {
dir = Vector3.zero;
dir.x = -Input.acceleration.x;
dir.z = 0;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
Debug.Log(dir.x);
if(Input.touchCount==0&dir.x>0&isRunning==false){
StartCoroutine (leftAnimation());
}
else if(Input.touchCount==0&dir.x<0&isRunning==false){
StartCoroutine (rightAnimation());
}
//This limits the movement for the Left
if(transform.localPosition.x>minPos){
transform.localPosition = new Vector3 (minPos,transform.localPosition.y,transform.localPosition.z);
}
//This limits the movement for the Right
else if(transform.localPosition.x<maxPos){
transform.localPosition = new Vector3 (maxPos,transform.localPosition.y,transform.localPosition.z);
}
}
IEnumerator leftAnimation(){
isRunning=true;
animation["left"].layer = 5;
animation["left"].wrapMode = WrapMode.Once;
animation["left"].speed = 2;
animation.CrossFade("left",0.3f);
yield return new WaitForSeconds(animation["left"].clip.length);
isRunning=false;
}
IEnumerator rightAnimation(){
isRunning=true;
animation["right"].layer = 5;
animation["right"].wrapMode = WrapMode.Once;
animation["right"].speed = 2;
animation.CrossFade("right", 0.3f);
yield return new WaitForSeconds(animation["right"].clip.length);
isRunning=false;
}
}
How to edit this code to make the object move also in the y-axis?