Using the accelerometer for collecting

Hi, I am trying to convert the “Roll a ball” tutorial game to be used on Android. I found a tutorial on youtube about how to use the accelerometer and I am trying to make it fit in the existing Player Controller script, so I can collect objects by twisting the phone. I am not sure that it is a good idea, but I would like to try it out.
I have very little sense of C# particular where to place what in the different orders for different void statements. I did make the code work without the collect stuff and inserted speed, but then I moved very slow and no collection of course.
I get no error but also no movement at all, speed was set to like 10 or 50.

public class PlayerController : MonoBehaviour {

     public float speed;
     public Text CountText;
     public bool isFlat = true;
 
    private int count;
    private Rigidbody rigid;

     public
    void Start ()
         {
         count = 0;
         SetCountText ();
         rigid = GetComponent<Rigidbody> (); 
         }
     public
     void FixedUpdate()
     {
         Vector3 tilt = Input.acceleration;
         if (isFlat)
             tilt = Quaternion.Euler (90, 0, 0) * tilt;
         rigid.AddForce (tilt * speed);
     }

     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag ("Pick Up"))
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
             }
     }


     void SetCountText ()
     {
         CountText.text = count.ToString ();
     }
}

First, it’s important that you use code tags when sharing code . The code you’ve posted is difficult to read. Code tags make it much easier on us.

It’s one thing to find and explain syntax errors in code. I never mind doing that. It gets a bit more questionable with logic errors. My thinking is that if I tell you how to do it, you didn’t actually learn anything. Note that I haven’t used accelerometer myself, so it’s not like I’m withholding this information from you and waving it over your head… I’d have to look up the specifics myself to be able to say for sure, but you can do the same thing, so why would I steal that experience from you?!

Copying and pasting code, whether it’s from a tutorial or a fully-functioning script, is not a great idea. You kinda need to know what each line does and why you’re using it. That’s the only way you can get better and get yourself to the point where you can do this stuff all on your own. When you run into a line of code that you don’t understand its purpose, look it up in the API (or a C# reference if it’s not Unity-specific).

Beyond the syntax of the code you’re using, understand the logical purpose of each line. Especially when you’re combing content from two different resources as you’re doing here. So, for example, the variable “isFlat” you’re using. What is its purpose? Why is there code inside an if block checking its true status? Can it ever be false? (It doesn’t look like it from what you’ve shared).

Programming isn’t just a set of commands you string together to make things happen. It’s training your brain to work a specific way to solve problems one logical step at a time. That way of thinking isn’t natural and takes a lot of time and practice to really zero in. Don’t feel bad for not understanding it immediately. Just keep at it until it all starts to click. And it will eventually click if you stick with it.

Maybe go back through and see which parts, specifically, you don’t understand the function of. Read the appropriate pages of the API documentation and see if it makes more sense. If it doesn’t, try writing the same thing using concepts you do understand. And if you don’t know the right words to make that happen, Google how to do that very specific thing (“how to use accelerometer input in unity”, for example) and read the first 20 links you get back until you get the idea. I’ve only been programming for about 6.5 years now, but I’m pretty good. And yet I still have to look stuff up almost every single day. It’s a lifelong journey!

Thx, sorry about the code thing, I have tried to fix it.
I do understand the most of the single sections, but I have very little sense for the overall setup, are the code sections placed correct relative to each other? When I try to combine from different tutorials, I am unsure where to put what. I think I need to find and understanding all the void’s tutorial, lol.

I actually try to listen and understand the tutorials, and concerning “isFlat” I did think it was useless, but I better start with what they tell and then try changes afterwards. As I understood it, it should mean that the starting point is that the phone i lying down flat, like on the table position? Changing the axis’s in oppose to in front of you as explained here

from like 4:00 and on.
Taking this code out made no change. I could not find it in the Unity - Scripting API: .

There is no way I can guess the code creators logic, if I cannot use youtube tutorials and ask in here, I dont know how to move forward? Unity’s own tutorial on accelerometer: Getting Mobile Input - Unity Learn does not explain enough to make it useful for me.

I checked the accelerometer code separately first, which worked, but very slow. Then I add on, isn’t that the way to work, first check the single peaces?

Also I think I better get the Unity remote thing, copying manually over every time is time consuming.

BTW thx for cheering up kind of comments :slight_smile:

Not a problem! We all need a bit of encouragement now and then!

This sounds to me like you still need to focus on the very basics before messing with the accelerometer stuff. You keep referring to the “voids”, but do you know what that keyword means? Do you have a good understanding of the fundamentals of object-oriented programming, like classes, instances, properties, methods, etc?

Going through boring stuff like that sucks, and I know you want to get to the more exciting things that feel like games. But focusing on the core material first will be invaluable in learning that stuff later on.

Yes, that is the right methodology. But again, I think it’s your lack of understanding of the fundamentals that’s causing the problem here. It’s not so useful to understand the separate pieces if you don’t really get how they all fit together to make things work.

I’m not positive if the accelerometer works in the Remote app. When interfacing with mobile hardware, I think you’re kinda stuck building to the actual device.

hey hey I do know some about object-oriented programming, I even had a class about logical gates in processor once, but that is a long time ago.
If you know a good resource for C# beginners please let me know, I can do what it takes, but using all my time searching in forums, where I don’t know the quality of it and half of it don’t work anyway seems waste of time.

Also I do hope to learn it with a concrete project, it is more motivating and sticks better when you can relate it. As you said I cannot build Rome in one day. To me getting it in small peaces with the project would have been fine.

I’ve heard good things about the C# Yellow Book. There’s a link on that page for a free download of the book in PDF format.

Also please note that I wasn’t trying to be critical. It’s just my opinion that it seems like you could use a refresher on the theory of it all.

1 Like

Thx, I will have a look at it.
Def. needed, your comments was appropriate. It is always hard to sense the tone via text. Also I need to figure how much time to use trying, before going to the forum. I needed a testing routine.

For testing I set up two players and scripts for comparing and trying. I think it was the speed thing and the Isflat, thx, so I deleted the Isflat and made a physic constraints instead, and now it works - see soon I will do the new Overwatch, lol (not). Though if I need it to jump, I might have a problem.
Actually I am supposed to just be the animator, lol. Working alone I need to do it all.

Just one more thing, am I correct using the FixedUpdate instead of just Update, since it is physics?

I’ve never used FixedUpdate for input. FixedUpdate runs at a guaranteed fixed interval, so it is useful for physics, yes, but you can also just work inside of Update and multiply by Time.deltaTime. I’ve never seen a definitive ruling on this one way or the other that I can think of.

1 Like