Hi guys,
I’m struggling to implement a moving left and right mechanic with the accelerometer. With a clamp so you can only move left and right 150f’s or something.
So far my code moves backwards and forwards invertedly. My character moves left and right very slightly but I think thats a bug.
transform.Translate(Input.acceleration.z, 0, 0);
Thank you.
I’m having physics problems in my own thread as well , but I know how to use the Accelerometer. Make sure the object it’s attached to has a rigidbody.
using UnityEngine;
using System.Collections;
public class carController : MonoBehaviour
{
public float carSpeed;
bool currntPlatformAndroid = false;
Rigidbody2D rb;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
#if UNITY_ANDROID
currntPlatformAndroid = true;
#else
currentPlatformAndroid = false;
#endif
}
void Start()
{
position = transform.position;
if (currntPlatformAndroid == true)
{
Debug.Log("Android");
}
else
{
Debug.Log("Windows");
}
}
void Update()
{
if (currntPlatformAndroid == true)
{
//android specific code
//TouchMove ();
AccelerometerMove();
}
else
{
position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
transform.position = position;
}
position = transform.position;
position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
transform.position = position;
}
void AccelerometerMove()
{
float x = Input.acceleration.x;
Debug.Log("X = " + x);
if (x < -0.1f)
{
MoveLeft();
}
else if (x > 0.1f)
{
MoveRight();
}
else
{
SetVelocityZero();
}
}
void TouchMove()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
float middle = Screen.width / 2;
if (touch.position.x < middle && touch.phase == TouchPhase.Began)
{
MoveLeft();
}
else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
{
MoveRight();
}
}
else
{
SetVelocityZero();
}
}
public void MoveLeft()
{
rb.velocity = new Vector2(-carSpeed, 0);
}
public void MoveRight()
{
rb.velocity = new Vector2(carSpeed, 0);
}
public void SetVelocityZero()
{
rb.velocity = Vector2.zero;
}
You can modify the clamps to your liking. Also , help me out on my thread!
(Repost to correct thread) Hey guys , I have a script attached to a car game object that allows the player to tap the screen and then accelerate , it works fine in Unity Remote , but when I download the APK , as soon as I tap the screen , collisions...
Reading time: 7 mins 🕑
Likes: 4 ❤
hey guys.
I want to play some particles when I move my Android device and stop the particles when I stop the movement of the device.
https://forum.unity.com/conversations/get-unity-device-movement.577097/