Unity 2D GameObject(s) Rotate Randomly - unity3d

Good evening everyone,
I hate to ask this question but I am new to Unity and I'm running into an error(I think) that is really unexpected. I actually just started work on my first 2D game tonight and cannot find anything online that seems to explain this.
Long story short, I have a character (GameObject) that simply moves from the left to the right on a platform, then he should fall (or jump) onto the next platform and keep moving until he falls into infinity. I have been able to get my character to move from the left to the right, but when he reaches the midway point between the two platforms ... the platforms begin to rotate counter-clockwise. I'm including screenshots below to show what I mean. Has anyone seen this before?
My player has the RigidBody2D and Box Collider 2D Components
My platforms have the Box Collider 2D Components
This is the code I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb.velocity = new Vector2(1, rb.velocity.y);
if(Input.GetMouseButtonDown(0))
{
rb.velocity = new Vector2(rb.velocity.x, 10);
}
}
}
and these are the photos of the game as it plays out:

The problem of your game isn't the character but the platforms!
On each platform you have a Rigidbody2D and a box collider 2D so the player can walk on them and collide.
On unity physics is always working and so even if you set the gravity to zero the player has gravity and so it will push the platforms and make them rotate
You can easily fix this by going to the constraints of the rigidbody2D of each platform and check the freeze rotation on the Z axis. It will stop rotating

Actually the platforms don't move...
But you have connect the camera Transform to the player transform; So when the play rotation changed(When he falls) The camera rotation changed with the player too
You can fix this issue by making the camera follow the player position via code, This video could help you

Related

How do I detect collisions in a 2D project in Unity (without using a RigidBody2D)?

I'm trying my hands on Unity for the first time and, as a "hello-world" kind of project I'm aiming for a 2D "Asteroids" game.
I'm advancing well, but the collisions are causing me headaches. They don't register. I've tries with and without a RigidBody. But The RB forced me to set it to Kinematic as it would otherwise fall down due to gravity, which is not what I want in a top-down shooter.
I tried setting "Full Kinematic Contacts" without any luck.
There are many different possible combinations and I can't find the right one. Rigid Body or not? Collider or not? Maybe both?
Here's a little demo project that reproduces the issue. I would expect it to show a log-line in the console when the circle touches the square. But it does not. How do I get that done?
https://github.com/exhuma/unity-collision-test
The RB forced me to set it to Kinematic as it would otherwise fall
down due to gravity
You may uncheck the gravity in Rigidbody component. (Gravity Scale on Rigidbody2D)
Rigidbody is a way to contact with physics.
Rigid Body or not? Collider or not? Maybe both?
Add both.
I had viewed you code on github and I would like to suggest
Use Velocity
rigidbody.velocity = new Vector2(300 * Time.deltaTime, 0);
instead of
transform.Translate(Vector2.right * Time.deltaTime * 3);
and also add
public Rigidbody2D rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
}
The script attached gameobject must have Rigidbody2D
Here is the doc link:
https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
Thanks

How do get a 2D sprite to face the camera in a 3D unity game?

I've been trying to get it to work but most of the codes are either outdated or doesn't work can anyone help i need a code for unity 2021 ?
I tried finding some codes but they are pretty old like from 2016
The term "billboard" in computer graphics refers to an object that always faces the camera. You can try a billboard component, like so:
public class Billboard : MonoBehaviour {
public Transform cam;
private void Start() {
cam = Camera.main.transform;
}
void LateUpdate() {
transform.LookAt(transform.position + cam.forward);
}
}
A follow camera behaviour should always be implemented in the LateUpdate because it tracks objects that might have moved inside the Update. Also if your sprite is inside a canvas make sure its in world's space, so that it is a 3D world object and it can rotatute. Canvas space is an option in the canvas component itself.

Player Follow Camera Goes Inside Objects

I was working on a 3d game where the camera is continuously following the player object in position and rotation fields.
Complete 3d environment is set up as per my gameplay. A player moves and rotates in different areas of the environment, I was getting this kind of abnormal view in front of the screen. The camera goes into the other objects.
This is a really poor gameplay experience so how to fix this?
How to make this look better when the player following the camera goes inside other environment objects?
You can make an empty gameobject inside the player, then your camera script should not follow the player, but it should follow the empty gameobject instead.
if you define an offset, it would be best practice
camera script is as below:
public class Followplayer : MonoBehaviour {
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update () {
transform.position = player.position + offset;
}
}

There is no reason for this Unity script to make the sprite move, yet it moves

Here's the code:
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody2D rb;
public float strength = 100f;
void Start () {
//Initialize the body of the sprite so that forces
//can be applied.
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
//Note2Self: var says this is a variable of unspecified type.
var touch = new Touch();
/*
if (touch.phase == TouchPhase.Began){
rb.AddForce(transform.forward * strength);
}*/
if (Input.anyKey)
rb.position.Set(0, 100);
}
}
I was trying to practice some basic stuff in Unity (I am not used to programming in IDE's at all, we've just used vim in my program so far) when I happened upon this oddity.
First, I didn't understand why the sprite would move at all when there can't be a touch identification, since I haven't actually tested this on a mobile device. So I commented it out and for some reason the sprite still moves. That code shouldn't be doing anything, yet it is.
I have checked to see if the sprite is using the up-to-date script - it is - and I have checked if the script is targeting the correct rigid body and that it is a rigidbody2D. It is.
What is going on?
If it is just falling it is probably effected by gravity.
You can turn this off in your script by adding rb.gravityScale = 0; at the end of your Start() function
OR
by setting it in the editor inside the rigid body component
I looked in the unity documentation:
https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
which says that applying a Rigidbody2D component to an object will place it under control of the physics engine.
The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces.
I have run into issues with rigidbodies on more than one occasion, I suggest you check the RigidBody2D component in the unity inspector window and make sure to uncheck use gravity.
Also, you may want to just write a custom script without using a rigidbody. Doing a search on youtube will probably give you exactly what you need for that. Hope this helps!

Camera rotation within a prefab

I am trying to create a multiplayer game in unity3d using a diablo like camera system. So wherever I click on the screen. The system it was working fine in singleplayer where I did not need to include a camera in a player prefab. However Now I am facing the problem where my camera rotation is also affected by the rotation of my prefab parent. The hierarchy looks like this:
There is a script added to the camera that looks like this:
using UnityEngine;
using System.Collections;
public class MainCameraComponent : MonoBehaviour {
public GameObject Reaper;
private Vector3 offset;
void Start () {
offset = transform.position - Reaper.transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = Reaper.transform.position + offset;
}
}
When I run the game the camera always stays behind my character, while I want it to always stay at the same rotation. so if I order my character to walk north I would see his back, if it would walk south I wanted to see the front.
notice how the shadow changes, (thus the rotation) but i always face the back of my model. TLDR: I want my child camera to ignore the rotational change of its parent and be static. whilst letting my camera position be guided by its parent. as far as I know it seems impossible to make a networkmanager instantiate a new player prefab and attach a camera afterwards on the same hierarchy level.
Any help would be greatly appreciated.
However Now I am facing the problem where my camera rotation is also
affected by the rotation of my prefab parent
That's because your Camera is a child of the player so it will do whatever the Player/parent is doing.
Your camera should not be under the player. Remove it and make sure it is not a child of the Player or any other Object. That script should work or make the camera follow the player without making the Camera the child of the GameObject.
Remove Player = GameObject.Find ("Player"); from the LateUpdate function. You have already done this in the Start function, so it is unnecessary to do it in the LateUpdate function. GameObject.Find ("Player"); should never be called directly from the LateUpdate function. It will slow down your game.
EDIT:
After reading your comment, it looks like you want to instantiate a player with the camera. You can do this without making the camera a child of your player.
Your current Setup:
Player
Model
Projectile
Projectile Mesh
Camera
So your camera is under Player.
You new Setup:
PlayerPrefab
Player
Model
Projectile
Projectile Mesh
Camera (Attach script in question to this)
Camera is now PlayerPrefab instead of Player. Now, you can instantiate PlayerPrefab and move the Player with a script. I don't know what your move script looks like but it should be attached to the Player not PlayerPrefab. The PlayerPrefab is used to hold everything so that it can be easily instantiated as one. The code in your question should be attached to your Camera.