Unity - Can I add CharacterController component to 2D sprite in 2D game? - unity3d

Up to this point I've been making 3D games in Unity and I've been using CharacterController.Move() for moving player most of the times. I'm currently making a 2D platformer which is the first time I'm doing a 2D project in Unity. I'm trying to figure out what would be the best way to move characters around. I think that it's better to write my own controller than to use rigidbody for movement. Rigidbodies may give nice smooth movement, but own controller gives me more flexibility and control over how moving characters work.
My question is: can I add CharacterController component to my 2D player and use its Move() method to move player? Or perhaps CharacterController should be used only for 3D games?

At the end the position of a gameobject is always given by the transform.position.
RigidBodies (RigidBody2D in your case) will handle the transform on physic update loop but at the end the transform position will be changed.
So if you don't want to use physic you can just set the target position to the transform position.
transform.position = newPosition;
If you want to use a rigidbody you can set the position like this
rigidBody2D.MovePosition(newPosition)

Related

How to determine when player (cylinder) has moved above a plane in unity 3D?

I am making a delivery game and temporarily designed my game so that the player who is a cylinder at the moment has to locate the delivery location which is a flat plane and move onto it in order to deliver the objects.
I have tried adding a 2d box collider to the plane to detect when the player collides with the plane but the collider only works on the x,y axis and i am not able to rotate it 90 degrees.
So my question is what is the best way to determine when the player has moved above the plane?
Here is a picture to demonstrate what i mean:
Before moving onto plane
How to i detect this:
Player is on top of plane
First, make sure at least one of the objects has a rigid-body component. If you are using a character controller to control your player, add the rigid body to the plane. If you don't want the rigid body to affect the plane, just freeze all the position and rotation constraints. Next, make sure both of the objects have a 3D collider, not 2D. Your game is 3D and needs 3D colliders. 2D is for 2D games such as sprites. (I recommend a cylinder collider for the player and a box collider for the plane.) Make sure the plane's collider "isTrigger" is enabled. This should work for you to detect the collision.

Unity composite collider 2d - Collission detection with onTriggerEnter or Raycast

I have found other people asking for this on the internet but I haven´t found a solution. I have a 2d game using tilemaps and composite collider. But when I add a composite collider I can´t use ontriggerenter or raycasts to detect the ground. So I have completely removed the ability to jump because there is no way of knowing if the player is on the ground or not, resulting in the ability to jump again before landing. Has anyone found a way around this?
There shouldn't be any issues with your approach. Without code examples all I can give you is some general pointers:
OnTriggerEnter
Use OnTriggerEnter2D(Collider2D col) to detect Trigger enters in a 2D environment.
OnTriggerExit2D for leaving, OnTriggerStay2D for every frame you're still colliding with other colliders.
Don't forget that one of the objects need to have a Rigidbody2D (if you don't want to use physics, select "Kinematic" in the dropdown and check the "Use Full Kinematic Contacts". For improved collision detection, change the Collision dropdown to "Continuous".
Raycast
When using Raycasts you need to, either start the raycast from a position that doesn't include your own hitboxes, or make sure the raycast ignore its own layer.
// Raycast down ignoring Player layer
int layerMask =~ LayerMask.GetMask("Player");
RaycastHit2D hit = Physics2D.Raycast(transform.position,
Vector2.down,
layerMask: layerMask);

Unity: 2d GameObject Ricochet

How can I make a 2D GameObject Ricochet off a wall or surface with the same angle it used to hit it? Can you just point me to the right direction or how I should do it?
Create your bouncing game object as a sprite. Add a 2D collider component and a rigidbody2D component. Add a physics material into the material property of the rigidbody and adjust settings to taste. Your object should move with physics and bounce off things when it collides with them.

Capsule Collider doesn't move smoothly

I have some issues using CapsuleCollider and RigidBodyController in a Player GameObject in Unity.
Using a CapsuleCollider as collider for the player, I noticed that the player doesn't move like a capsule or a sphere (in other words, I want that the movement should be like a rolling ball, with no stuttering), but its movements like more like a box pushed, with stops and starts, that make some little oscillations of the camera.
Any ideas, solutions or tips?
realy that is all based on how your moving the object, and the rigidbody2D settings of the object, I think using physics forces would roll your character like a ball or capsule, however just setting a velocity wont.

Do I need a rigidbody for gameobject that scales up and down?

I have a beginner-like question. I currently have a gameobject in my scene that scales up and down via animation. It has a Circle Collider 2D on it.
I've seen some tutorials before regarding the performance optimizations of rigidbodies and colliders. I learned that if the gameobject should move in the game, it should have a rigidbody component. Otherwise, a collider component itself is fine for triggers.
Since my game object is kind of moving (because of the endless scale up/down animation), would it be best to put a rigidbody component on it?
I do like to mention that I'm not using any physics movement such as AddForce or anything like that. Hope someone can clear this up.
A gameobject should have a rigidbody component for mainly two reasons, first one is if you are using physics, second one is if you want other colliders to detect a hit/entrance/exit by that gameobject.
From what you described it does not seem like you would need a rigibody on your gameobject, especialy because the movement is only by scale and not by position.
and also, just to clarify, putting a rigidbody component on a gameobject is not a must even if the gameobject is moving. there are different ways to move objects, and rigidbody usually helps in case that the movement should be very realistic and interact with other gameobjects it is colliding with.