How to make a platformer in Unity? - unity3d

(I am a new person in stackoverflow, might ask a bit different)
How to make a platformer in Unity?
Unity 2020.2.0a11.1312.3
Info before start: I want to make a mobile game.
(with some buttons)
Hello, I want to make a platformer in Unity.
I tried to use Rigidbody2D.AddForce() method but I couldn't get it working.
Can somebody help me?

I don't usually work on mobile games in Unity but these are generally the reasons why rigidbodies don't work.
Make sure to connect the script to the object with the rigidbody by dragging the script on to the object.
Use RigidBody2D Rigidbody2D = GetComponent<RigidBody2D>() at the start of your script.
If you're using the AddForce() method in the Start() function then you need to move it to the FixedUpdate() function.
Sometimes the movement does work but it doesn't move the object very fast because there isn't enough force so you might have to increase the force.
Check to make sure that there are no movement constraints applied to the Rigidbody.
Since you're working with buttons you will have to link the buttons to the script with the rigidbody like:
public void OnPointerDown(PointerEventData eventData)
{
//Start moving logic
moving = true
}
public void OnPointerUp(PointerEventData eventData)
{
//Stop moving logic
moving = false
}
private void FixedUpdate(){
if(moving){
//Moving
Rigidbody2D.AddForce(force * Time.deltaTime)
}else{
//Not moving
}
}
Since OnPointerUp/Down is only called once when the event happens you need to put your start moving logic in OnPointerDown and stop moving the object in OnPointerUp.

Related

The "OnCollisionEnter" function isn't being triggered in one case but is being triggered in another case

I'm making a virtual reality application for the oculus platform with the unity engine & experiencing a problem in the process. For some reason, when i'm holding two objects (a hammer & a nail) with my controllers, between the two of them, the "OnCollisionEnter" function isn't being triggered, even though it's being triggered between any one of them & the target surface. Can someone please help me understand why it's happening? Is there a solution to it?
First make sure that isTrigger is disabled.
Then add Rigidbody to both objects.
Also be sure to use rigidbody instead of transform to move and change position.
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
rb.AddForce("some force vector");
rb.MovePosition("some move vector");
}
It seems that this is a recurring & unresolved issue related to how VR works with the unity engine.

Unity3D - How can I detect collison between my car object and terrain?

I am working on a driving game for a project. My issue is that I'm unable to detect collision (I want to place the car to a checkpoint whenever it goes off the road) between the wheels of my car and the terrain. I tried to use this simple script but it does not seem to work:
using UnityEngine;
public class CarCollision: MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
if (other.collider.name == "Terrain")
{
Debug.Log("We got off the road!");
}
}
}
Is it a 2d game? If yes, use OnCollisionEnter2D instead of OnCollisionEnter.
Is the script attached to the gameobject?
Are you sure that the terrain has a collider which is not marked as trigger?
Try copying and pasting this same thing in the main controller.
I hope it worked. Don't forget to mark my answer as accepted if it worked!

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!

Unity rigidbodies movePosition over network

I think this is a network setting problem,
I am having some issues with my game in unity and I have been at it for several days. I am using rigidbody.movePosition to have a object follow a vr controller. If I hold down a button I start to move and I can swing the object with my arm. everything works perfect in regular testing. However, once I tried to use networking the rigidbody freezes once I push the button. when i stop pressing it snaps back to place.
couple notes. my rigidbody is in a seperate empty. kinematic is on. My problem is fixed if i turn off kinematic but the object floats away.
private Rigidbody r;
public SteamVR_TrackedObject controller;
void Start () {
r.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
r.MovePosition (controller.transform.position);
r.MoveRotation (controller.transform.rotation);
}

How to get an OnTriggerEnter2D to recognize objects entering it?

I am trying to create am endless jumper and I want to use a Cube to detect the Platforms and destroy them.
The problem I am having is that the Triggers aren't being recognized by these cubes.
The green is the destroy Cube.
I have the RigidBody2D and BoxCollider2D on the destroyer.
public class DestroyPlatform : MonoBehaviour {
void OnTiggerEnter2D (Collider2D collider)
{
Debug.Log("I am in the destroy Wall");
}
}
This is the code I am using. As I mentinoed I just want it to tell me it triggering. I have seen tons of code on how to destroy it, but this basic code isn't working.
All of the platforms are 2D Sprites and are using the following:
Notice both GameObjects are triggers. I have also tried to put the script on the platform prefabs and the destroyer cube.
Thanks for the help.