Accelerometer moving limit. Help me please!
Please help me! I had a problem with my final project game 2D, i tried to use accelerometer control for my project, but i had a problem, my character game (motor) still can move out of the track if i tilt my android device. if i tilt to the left, character wil get stuck on the left of track, if i tilt to the right, character will be out of the track. i want make a limit position for the character but i dont know the code to make limit access for my character, i tried many different way on youtube to make that happen, but still didnt work. if somebody can help me, im very thankful for that :) please...
this is my script character controller:
using UnityEngine;
using System.Collections;
public class motorController : MonoBehaviour {
public float motorSpeed;
public float maxPos = 2.5f;
Vector3 position;
public uiManager ui;
public audioManager am;
public audioManager ad;
private scoreManager theScoreManager;
void Awake() {
am.motorSound.Play();
}
// Use this for initialization
void Start () {
position = transform.position;
theScoreManager = FindObjectOfType<scoreManager>();
}
// Update is called once per frame
void Update () {
theScoreManager.scoreIncreasing = true;
// Coding accerleration move character
float temp = Input.acceleration.x ;
if ((transform.position.x < 2.5 && Input.acceleration.y > 0) || (transform.position.x > -2.5 && Input.acceleration.y < 0))
transform.Translate(temp, 0, 0 * motorSpeed);
}
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.tag == "mobil musuh") {
Destroy (gameObject);
ui.gameOverActivated();
am.motorSound.Stop();
ad.crashSound.Play();
theScoreManager.scoreCount = 0;
theScoreManager.scoreIncreasing = false;
}
}
}
THIS THE PICT MY GAME 2D:
Answer by JedBeryll · Dec 05, 2015 at 11:28 AM
Before moving your object you should check where it is going to move when applying force. so the right way of doing this is:
get motor position
get input.acceleration
add those together and check if the bike is going to be in the track
if it is, move the bike to the position
can u made the correct script accelerometer control for my project? because i still dont understand, i just wanna move the bike left and right, but i want the bike do not cross the track so i made max position left and right is -2.25, 2.5. thx u so much! sorry if my english so unwell :)
i never used input.acceleration so i would just be guessing
Your answer
Follow this Question
Related Questions
Moving to certain direction 1 Answer
WWW WP8 Cache? Maybe requests limit? 0 Answers
How to clamp transform.Rotate values? 0 Answers
Detect when near edge of an object (3D) 0 Answers
Como posso mover GameObjects de mesmo script de forma independente? 0 Answers