- Home /
Question by
ro88y88 · Jul 04, 2019 at 01:50 AM ·
accelerometertiltbalance
I need to add resistance to my balance app.
Have a balance game and i need help with the code. right now the way the code is the objects can be held in one spot with no resistance. i would like for the objects to tilt on there own to make it harder.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class acclEM : MonoBehaviour {
//public float speed = Random.Range (7,30);
public float speed = 5f;
void OnEnable()
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
void LateUpdate (){
float temp = Input.acceleration.x;
transform.Rotate (0, 0, -temp * speed);
}
}
Comment
some simple random titl? like?
float unstable = 5f;
//public float speed = Random.Range (7,30);
public float speed = 5f;
void OnEnable()
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
void LateUpdate (){
float temp = Input.acceleration.x;
transform.Rotate (0, 0, -temp * speed);
if(temp<.01f){//if there is manual rotation disable random
float rand = Random.Range(-unstable, unstable);
transform.Rotate (0, 0, rand);
}
}
its what im looking for but its really twitchy is there a way to make it smoother?
Or maybe have it start going the the opposite direction after a couple seconds. I'm looking for the kinda balance like in a skateboard manual if you know what I'm saying.
hummm, probably you will need a more complex function, capable to iterate between a sequence of frames, some like?
public float SequenceDuration;//the duration between one change and another
float counter = 0f;
float unstable = 5f;
//public float speed = Random.Range (7,30);
public float speed = 5f;
void OnEnable()
{
transform.rotation = Quaternion.Euler(0, 0, 0);
RestartSequence();
}
void LateUpdate (){
float temp = Input.acceleration.x;
transform.Rotate (0, 0, -temp * speed);
//if there is manual rotation disable random
if(temp<.01f){
UpdateRandomSequence();
}
}
void UpdateRandomSequence(){
if (SequenceDuration < counter) {
RestartSequence();
}
//simple, clumsy lerping...
transform.Rotate (0, 0, rand*(Time.deltaTime/SequenceDuration));
counter += Time.deltaTime;
}
/// <summary>
/// Store the random value, exculsive initialized of this function
/// </summary>
float rand;
void RestartSequence(){
rand = Random.Range(-unstable, unstable);
counter = 0f;
}