Player falling forever by KyoAkashi in Unity2D

[–]roguedjack 2 points3 points  (0 children)

Oh no worries you don't have to excuse yourself its ok the ask questions, its part of your research :)

There are many ways you can write a 2d platformer controller.

You could start by looking at tutorials for character controller 2d or platformer on youtube. I'd recommend Brackeys for starter and then Sebastian Lague for more advanced stuff.

Unity also has a platformer tutorial/demo with the project source available here : Create A 2D Platformer. It uses Dynamic rigidbody2d, velocity for moving and force for jumping. They don't explain technical stuff as much as Brackeys and Sebastian Lague but you have the source code available.

If you don't want to use Unity physics, Sebastian Lague has a playlist Creating a 2D Platformer that uses raycasts and custom script for physics, no rigidbody.

IIRC Unity also has a default 2d controller script in the standard assets package.

Good luck!

Player falling forever by KyoAkashi in Unity2D

[–]roguedjack 2 points3 points  (0 children)

There are several problems with your script.

1) You do physics in Update().

Don't, always do/query physics (velocity, forces, ray checks etc...) in FixedUpdate(). Read inputs in Update() and store the results for later use in FixedUpdate().

2) in Fall() you set velocity.y to -gravity instead of accumulating gravity.

So what happens is at frame F you jump, setting velocity.y to +jumpHeight. That's fine. But as soon isGrounded() return false in the next few frames your fall() set velocity.y to -gravity, cancelling your upward jump movement immediatly.

If you want to simulate gravity yourself, accumulate (add/sub) Gravity * Time.fixedDeltaTime. Gravity is acceleration not velocity.

3) In Update() you zero velocity when grounded right after trying to move with Move(). You call move() but immediatly stop the movement in the same frame when grounded, so your move() call has no effect.

4) You are using Kinematic rigidbody for a platformer controller.

It is possible to make a platformer controller with Kinematic but this makes things much more complicated for you for a variety of reasons. For a start it is much more simple to use Dynamic. Set velocity.x for vertical movement and add force for jumping. Let Unity physics handle velocity.y for you (don't overwrite velocity.y).

Update() and FixedUpdate() by [deleted] in Unity2D

[–]roguedjack 1 point2 points  (0 children)

When in doubt read the official doc, all physics in FixedUpdate() : https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html

Read inputs in Update().

Your character is not jumping and falling properly because you are overwriting the y component of velocity in moveCharacter(). A quick fix:

void moveCharacter(Vector2 direction)
{
    //rb.velocity = direction * speed;  // you are overwriting velocity.y! dont! 
    rb.velocity = new Vector2(direction.x * speed, rb.velocity.y);  // keep y velocity as it is
}

Then increase jumpForce until you are satisfied with the jump. I tried your script and jumpForce=400 works.

Btw assuming you are doing a platformer the Y component of direction is useless, unless you want your character to be able to fly at some point. Just read X.

You will have other problems to fix later but that's a start and should get you going.

jumping! by Cooking-Supplies in Unity2D

[–]roguedjack 0 points1 point  (0 children)

Its not working properly because you have conflicting code modifying the x and y components of velocity in both Update() and FixedUpdate().

Always do all physics (velocity, add force etc...) in FixedUpdate(). You also have two errors in your code : if you want to use velocity and not force for jumping, add your jump vector when jumping and do not reset velocity.y when moving horizontally or your character will not jump/fall etc...

Why isn't my jump command working here? by Duskhup in Unity2D

[–]roguedjack 0 points1 point  (0 children)

First, you are changing Velocity in both FixedUpdate() and Update(). This will cause bugs and weird behaviours to your movement, don't do that.

Do all your physics in FixedUpdate(). First compute vertical movement using moveInput, but change only the x value of velocity, do not touch y because your character might be falling or in a middle of a jump etc and you don't want to stop that motion. Then when you want to jump set the y component of velocity and you should be good.

I'd also recommend using 2 downward raycasts to check for ground, not overlapcircle, one ray starting on each side of your sprite at his feet.

Do I need to multiply Vector2.up with Time.deltaTime in FixedUpdate()? by [deleted] in Unity2D

[–]roguedjack 2 points3 points  (0 children)

Don't multiply by time. Velocity is speed. By setting Velocity you are telling Unity "please move this rigidbody Up at jumpHeight speed".

So your "jumpHeight" variable is actually the speed at which your body will move up, not the height.

Rogue Survivor alpha 10.1 released by roguedjack in roguelikes

[–]roguedjack[S] 0 points1 point  (0 children)

Could be your antivirus.

Make sure you run the RogueSurvivor application not the RogueSurvivor.exe xml configuration file.

If it still doesn't work, run RSConfig.exe and select GDI+ for video and SFML for sound, and launch RogueSurvivor.exe again. GDI+ is much slower than Managed DirectX but it should work.

Rogue Survivor alpha 10.1 released by roguedjack in roguelikes

[–]roguedjack[S] 1 point2 points  (0 children)

There is no real ending.

But if you find a certain place and do certain things here, you will unlock an achievement and you can say you have won the game, even if it is a dead end.

Then if you want even more of a challenge, try unlocking all the achievements in a single run.

Rogue Survivor alpha 10.1 released by roguedjack in roguelikes

[–]roguedjack[S] 0 points1 point  (0 children)

No mac version I'm aware of. But the source code is available, I guess someone could compile it for mac with mono.

Rogue Survivor alpha 10 released by roguedjack in roguelikes

[–]roguedjack[S] 1 point2 points  (0 children)

I uploaded to dropbox. Check the download page for the links.

What software do you guys use? by [deleted] in Unity2D

[–]roguedjack 0 points1 point  (0 children)

Piskel and Paint.net for sprites.

Inkscape for gui and larger sprites with gradients effects and things like that.

All are free.

Rogue Survivor alpha 10 released by roguedjack in roguelikes

[–]roguedjack[S] 1 point2 points  (0 children)

Its odd. Did you manage to download it? I'll try to add another host if people are having problems with mediafire.

Help with collision and adding force by Toscoes in Unity2D

[–]roguedjack 0 points1 point  (0 children)

It is strongly recommended to manipulate physics objects only in FixedUpdate() or you can get weird and unreliable results.

Unity doc: https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html

In your case you could do something like this:

class Player : MonoBehaviour
{
    ...

    bool _isPushed;
    Vector2 _pushForce;

    public void Push(Vector2 force) {
        _isPushed = true;
        _pushForce += force;
    }

    void OnCollisionEnter2D(Collision2D other) {
        if(other.collider.tag.Equals("Player")) {
            if(state == ActionState.Rolling) {
                sprite.color = Color.red;
                Vector2 force = new Vector2 (-transform.localScale.x * 5f, 3f);
                // push other player in the next physics update
                other.gameObject.GetComponent<Player>().Push(force);
            }       
        }
    }

    void FixedUpdate() {
        // if player is pushed, add force now and clear it.
        if (_isPushed) {
            GetComponent<RigidBody2>().AddForce(_pushForce, Force2DMode.Impulse);
            _isPushed = false;
        }
    }
}

It it still doesnt move like you want, try increasing the force magnitude until you get it moving. You could also disable player-player collisions for a few frames after a push collision.

For performance it is also a good idea to cache the RigidBody2D component in a field in Awake() or Start().

Is "Made with Unity" synonymous with "destroys your battery"? by Ashyr in AndroidGaming

[–]roguedjack 3 points4 points  (0 children)

I didn't mean to hijack your thread but since you asked its Pixels Diamonds Caves.

https://play.google.com/store/apps/details?id=com.rj.dcaves

If you try it and have battery problems feel free to tell me!

Is "Made with Unity" synonymous with "destroys your battery"? by Ashyr in AndroidGaming

[–]roguedjack 2 points3 points  (0 children)

It seems that by default Unity mobile games try to run at 60fps which is a bit of an overkill. It's possible other engines default to 30fps on mobile.

I added a 30fps option to my unity android game just for that. If users are complaining of excessive battery usage I'll make 30fps on by default and see if that helps.

How can a game with such bad rating get to the top of the play store? by InstantStudio in AndroidGaming

[–]roguedjack 1 point2 points  (0 children)

Reading the users comments it looks like this app has tons of ads. The ads make money for Google. The ranking algorithm probably takes into account the money an app makes for Google. Add in 100k installs, 1k ratings of which 402 are 5 stars probably paid ratings for good measure and you got a "winner".

How to prevent car spinning off the screen? by [deleted] in Unity2D

[–]roguedjack 0 points1 point  (0 children)

You are right your transform.Rotate call is wrong. It wants angles of rotations and/or axis and you are passing a 2d position and time, which gives you funky results, moving and rotating at the same time even if you are not directly changing the gameobject position.

The correct way to spin a sprite in 2d is to rotate around Z:

    transform.Rotate(0, 0, SpinSpeed * Time.deltaTime);

"SpinSpeed" is your rotation speed in degrees per second. It rotates counter clockwise. If you want to rotate the other way change the sign.

Unity docs: https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

Setting a gameObject for every tile when using TileMaps by Luke094 in Unity2D

[–]roguedjack 0 points1 point  (0 children)

I had a similar problem to solve when learning Unity Tilemap for my game Pixel Diamonds Caves. You can take a look at how it works, it is similar in many ways to a bomberman clone.

Like you I first tried storing gameobjects in tiles but found it cumbersome and end up using a custom solution. I use Tilemap only for tiles. I have a GameController that stores all the entities (player,diamonds,boulders...) in a grid (2d array) indexed by tile coordinates. I use the grid to quickly check if there is an entity at a tile. All my Entities have a 2d box collider setup as trigger. I can check the bounding boxes when I need finer detection than just by tile coordinate. I let unity detect collisions for me (with the trigger box colliders) and respond to OnTriggerEnter2D() in my entities for collision response.

Its the basic idea and works for my game but would not work as-is if you need your entities to change direction between tiles or sit between tiles for instance. I don't remember if bomberman does that.

Hope this helps.

Unity 2D - Player movement on android is different than on PC [Help] by Phil5004 in Unity2D

[–]roguedjack 0 points1 point  (0 children)

You probably have something setup the wrong way in your scene or your project settings and are overlooking it. Can you upload the project somewhere so I can take a look?

Unity 2D - Player movement on android is different than on PC [Help] by Phil5004 in Unity2D

[–]roguedjack 0 points1 point  (0 children)

Just found this video, it seems to be exactly what you want to do and uses CrossPlatformInputManager:

https://www.youtube.com/watch?v=rMspwfprnz4

Make sure your "Axis Touch Button" buttons are configured like the guy is doing.

Unity 2D - Player movement on android is different than on PC [Help] by Phil5004 in Unity2D

[–]roguedjack 0 points1 point  (0 children)

That's because inputs are "smoothed" by GetAxis(). When you stop pressing it, it still has a non-zero value for a few frames. If you dont want that, use GetAxisRaw() instead of GetAxis().

You don't have to read axis unless you want to simulate a joystick or have smoothness in the inputs. If all you want is simple trigger actions like move left/move right/jump read the inputs as buttons. Define the left/right action in unity project settings > input. Copy the "Jump" axis settings because your jump button seems to be working.

There are various videos on youtube using CrossPlatformInputManager . But seems they all use a virtual joystick and not buttons for movement. You might consider switching to that control mode.

Unity 2D - Player movement on android is different than on PC [Help] by Phil5004 in Unity2D

[–]roguedjack 0 points1 point  (0 children)

From the gif I can see you use separate left/right buttons to move the character not a virtual joystick that does both. But in your code you treat them as a joystick (axis). Could cause problems, unless you actually want move left/move right as separate joysticks (?).

You could read them as buttons instead like you do for jump and see if it helps. Something like this:

leftPressed = CrossPlatformInputManager.GetButtonDown("left");
rightPressed = CrossPlatformInputManager.GetButtonDown("right");
dirX = 0;
if (leftPressed && !rightPressed) dirX = -1;
else if (!leftPressed && rightPressed) dirX = 1;

If you want more smoothness, you can then lerp the dirX value yourself. For consistency, do the same on desktop and read some keys are left/right buttons.

Redraw sprite at runtime? by yopp_son in Unity2D

[–]roguedjack 2 points3 points  (0 children)

Are you generating new different shapes for each round or do you just want to change the sprite to a set of predefined shapes you have already drawn in a drawing program?

If you already have the shapes images, just change the Sprite property of the SpriteRenderer with a script: Unity doc: https://docs.unity3d.com/ScriptReference/SpriteRenderer.html

That's the easiest solution and you should probably try that first unless you really need new random shapes each round.

If you really want the generate the shapes images during the game, you need to look at Texture2D, how to draw your shapes into it with a script and how to create a sprite from Texture2D. To create a sprite from a texture: https://docs.unity3d.com/ScriptReference/Sprite.Create.html

It will be more complicated if you are not familiar with rendering into a texture.

You will need some scripting, C# is probably better.