Hi there, I’m currently trying to make a fun 2D game as a project in my spare time. I’m very new to game development and unity as a whole. I got the ball to continuously bounce on my platform and it was working perfectly fine. However, when I added my movement script that will allow the player to tilt their Andriod screen. the ball no longer bounces. I think the script is interfering with it, but I don’t know what’s wrong.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
Rigidbody2D rb;
float dirX;
float moveSpeed = 20f;
void Start () {
rb = GetComponent<Rigidbody2D>();
}
void Update () {
dirX = Input.acceleration.x * moveSpeed;
transform.position = new Vector2(Mathf.Clamp(transform.position.x, -7.5f, 7.5f), transform.position.y);
}
private void FixedUpdate()
{
rb.velocity = new Vector2(dirX, 0f);
}
}
That is my code and as I said before, the ball was bouncing before I added this script onto it. What should I do to fix this?
Break line 17 apart so you can reason about all the parts involved. Right now it’s just a mess of hairy code.
How to break down hairy lines of code:
http://plbm.com/?p=248
Once you have done that, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run?
- what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
Also, never set the transform.position directly for Rigidbody (or RB2d): that is a “Teleport” to the physics engine and you will cause all kinds of malfunctions. Instead, use .MovePosition() to set new positions with physics.
Thanks for the tips
I’m still very confused though, and I don’t really know how I should be breaking up codes, I’m new to this and am still learning what most of the lines of codes mean. I understand the basic concepts of how coding works such as the “If this, then that.” But, I don’t know how to actually type the code and understand what each of them means.
Is there someplace I should start before jumping into trying to make a game? I thought this wouldn’t be too hard as I just want to make the ball bounce from platform to platform by tilting the screen.
There are plenty of places! Work through a few youtube videos on making games. Unity makes good tutorials and so did a fellow named Brackeys, but he no longer makes new ones. There are plenty of other small game tutorials. Start small.
And when I say “work through” I don’t mean blindly mimic the actions of what the guy in the video did.
I mean “Actually understand all parts of what he used.” Look up stuff in the online documentation. Compare it to other tutorials you have seen. Make experimental changes to what the video tells you to do. See what those changes do. Reason about the underlying engineered mechanisms involved.
Alright, thanks. I’ll see if I can find some good videos and whatnot, I’ll make sure not to just completely copy someone else’s code, most of the time I might not even want to make the exact thing as them anyways. Thanks again!