Hi everyone, I’m designing an app to move the object based on the phone. For example, when the user squats with the phone, the object will move down a certain distance and vice versa.
I’m trying to make a game like temple run. So when there’s a log is coming towards the user, the user needs to squat down to avoid it. I have no experience in creating a game using Unity3d yet. So I’m trying to do a simple function for the squat and stand function first.
In my game, the camera will be in first person view. So when the person squats, the view will go down as well. So I’m trying to create a function for the accelerometer to sense it and convert into the game first.
However, for every movement, there is an acceleration and a deceleration. If I translate all into movement, the character will go down and up instead of just down.
Can someone help me please? I’ve been stuck for few days trying to figure out the accelerometer.
Weird. Nothing moved how hard I tried to move my phone around. Anyway, when I hold my phone infront of me, the acceleration value is close to -1. So I tried something like:
ah yes sorry when i said “it should work” i ment the logic.
what exactly do you want? i meant beside from move a bit when your turn your phone.
maybe a grid would be good to move around in? maybe lerping to some positions would be better? al depends only what kind of game you are making.
I’m trying to make a game like temple run. So when there’s a log is coming towards the user, the user needs to squat down to avoid it. I have no experience in creating a game using Unity3d yet. So I’m trying to do a simple function for the squat and stand function first.
ok so now I’m a bit confused… isn’t squatting down/standing up an animation? temple run has three lanes you can move in, left right and middle, and in these 3 fixed lanes you can jump or crouch/squat. the character never moves up or down (over Y), just forward.
also using the acceleration function here isn’t the best idea, since you want a constant steady look over your screen, without that you would die to easy.
In my game, the camera will be in first person view. So when the person squats, the view will go down as well. So I’m trying to create a function for the accelerometer to sense it and convert into the game first.
and you are sure you want to stick with the user tilting his phone?
Unless you’re doing it first person, i would suggest making the cam follow the character’s head or one of the bones that go down and using it’s animation to make the cam move.
Just 2 general things, i think if you make a fast paced reaction game like temple run, i wouldn’t swing the cam around to much nor the phone
else the way you’re going at it with the last code you posted is the right direction, just filter out value of the accelerometer to create different cases of behaviour.
only you don’t really need the translate parts if you give it a fixed position right after.
It seems that the accelerometer will fluctutate every time the phone moves down. So I can’t translate all the movement into the object and 1 squat will make the object go down and up. Is there a formula to just detect the moving down and ignore the rest for 1 movement?
sorry i don’t understand your problem, can you describe a bit more precise what’s not working for you?
the accelerometer has value from -1 to 1 over it’s axis.
if you put the phone down on a table with the screen pointing up you get 0,0,-1
if you put it down with the screen towards the table you get 0,0,1
if you put it on it’s side with the screen pointing right you get 1,0,0 with the screen pointing left -1,0,0
if you put it upright with the screen facing you you get 0,-1,0 with the screen pointing away from you 0,1,0
off course moving the phone random won’t give you clean results.
anyway as i understand you want to filter out a value over a certain axis, so you’re logic is correct…
choose and axis to listen to but only between -n value and n value…
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AccelControl : MonoBehaviour {
public float tiltThreshold;
public bool isSquating;
public Text text;
void Update ()
{
if(Input.acceleration.y <= tiltThreshold)
transform.position = new Vector3(0,-1,0);
else
transform.position = new Vector3(0,0,0);
text.text = Input.acceleration.ToString();
}
}
Ignore that code for now. I’m implementing a new code.
void Update()
{
transform.position = new Vector3(0, Input.acceleration.y, 0);
}
Because for every movement, there is an acceleration and a deceleration. If I translate all into the object, the object will move up and down as the values will go from -1 to 0 to 1 to 0. The thing is that, I just want the value where it shows the object moving down. I can’t think of any way to do this. I tried low pass filter as well. It just smoothen out the motion.
If I do this, the character will still go down and up. Just that, it doesn’t go up as high as before. This doesn’t solve the problem though. And what if I want to stand up? My character will be stuck since you isolate it to be below 0
i tried answering this question above.
only the down value would be anything below zero.
but it’s all quite simple, i think you are over thinking it.
as i understand what you want:
tilting the phone, rotating the screen towards you, you want a character to crouch/squat
tilting it back to the original position the character returns to his stand up position.
first of all this is a task for the animator, you filter out the desired values over the axis you want and when in desired range you set a value in the animator to play the animation.
Yes, it will work by tilting. However, I’m designing this game to be an exercise game where the user needs to squat instead of tilting. You have to try it by yourself in order to know what I’m looking at. It will have the “bounce” issue for each movement due to the accelerometer.
Try holding your phone and move up/down but keeping the same orientation.
ah now i understand, you want the Player to squat in real life lol
ok it’s late now, i will give it a try tomorrow.
but my first guess is you’ll need more than the accelerometer to do that.