简体   繁体   中英

Move/roll ball with accelerometer

So I'm currently starting with programming, and I'm fresh to this topic.

I did start with the Unity game engine; people say it's not the best way to start but whatever.

I made the first game with the basic tutorial of unity.

Tho I can't yet really understand the complexity of C#. (Using Visual Studio, not sure if I should switch to sublime and how)

This game is about moving a ball around and collecting stuff. On the PC, it works just fine with the AddForce and Vector3 movement on the arrow keys. Though I wanted to try making this game for a mobile device, I thought about instead of typing the screen I might use the gyroscope of the mobile device. I found the "gyro" variable(?) in the Unity API documentation, but I don't really know how I define it just to move x and z-axis, so the ball won't starts flying off the table. I tried with the accelerator variable(?), but exactly this happened, even tho y-axis was set to 0. The following code is what i have at GameObject "player" so far:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class AccelerometerInput : MonoBehaviour
{


public float speed;
public Text countText;
public Text winText;

private Rigidbody rb;
private int count;

void Start()
{
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText();
    winText.text = "";
}

private void Update()
{
    transform.Translate(Input.gyro.x, 0, -Input.gyro.z);
}
void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    rb.AddForce(movement * speed);
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Capsule"))
    {
        other.gameObject.SetActive(false);
        count = count + 1;
        SetCountText();
    }
}
void SetCountText()
{
    countText.text = "Count: " + count.ToString();
    if (count >= 8)
    {
        winText.text = "You Win";
    }
}

}

I'm new to all of this, especially coding, anything that helps me understand how the language is interpreted will be much appreciated! Thank you!

I did start with the Unity game engine, people say it's not the best way to start but whatever.

That's right. There are many C# tutorials online. Just understand basic C# stuff and you should be fine in Unity. If you don't do this, there will be limitation on things you can do with Unity.

To answer your question, you need the accelerometer not the gyro sensor. Also, remove transform.Translate(Input.gyro.x, 0, -Input.gyro.z); from the Update function. Do not move Objects with Rigidbody via transform.Translate or else, you will run into issues such as no collision.

Something like this should do it:

Vector3 movement = new Vector3(-Input.acceleration.y, 0f, Input.acceleration.x);

You still need a way to detect if you are building for mobile devices or desktop. This can be done with Unity's preprocessor directives .

void FixedUpdate()
{
    Vector3 movement = Vector3.zero;

    //Mobile Devices
    #if UNITY_IOS || UNITY_ANDROID || UNITY_WSA_10_0 
    movement = new Vector3(-Input.acceleration.y, 0.0f, Input.acceleration.x);
    #else
    //Desktop 
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    movement = new Vector3(moveHorizontal, 0f, moveVertical);
    #endif

    rb.AddForce(movement * speed);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM