Hello, hello!
As the title suggests, I started about a month ago looking into making a little video game.
I’m totally aware of how hard it can be, so I decided to make something pretty basic that wouldn’t require too many constraints, which is why I chose 2D Physics and a vehicle (movements on a 2D plan, no animation whatsoever).
I’ve got some knowledge in 3D modeling (Cinema 4D) as an amateur, but I suck at 2D, I just can’t draw anything, or it takes me forever and I’m usually not satisfied with the result, so making textures is a painful tedious task.
First of all, I’d like to apologize in advance as I’m a complete beginner, like, I know nothing about Unity, I can’t even figure out (yet) how shaders work, or to move the axis of an object (like in C4D, which is very useful to rotate objects or even reset it without impacting the mesh) although I read it wasn’t possible, which I hope isn’t true.
Basically, I’m not here to ask you to tell me how to do everything, which would be verily boring, but I’d like to share my experience and steps through that “adventure”. So please don’t give me a “RTFMN”.
Of course, I’ll appreciate your help once I get really stuck (very likely to happen, right?).
When I started that project, there are 2 things that I absolutely wanted to figure out:
- how to make a vehicle and see how hard it’d be
- if it was possible to use a 3D object with 2D physics (sorry, I don’t know how to call that, as saying "Unity 2D’ sounds quite improper)
For 1), thanks to all the tutorials on YouTube, I quickly got my answer.
For 2), importing FBX files works pretty well except that there is something really strange about the axis, as it seems that the model has to be oriented in the right way beforehand, or else even having rotated it won’t let us to put the Wheel Colliders as we want (the wheels get aligned on another axis, not the object’s). Also, the ways the materials work are… peculiar to me.
Needless to say that I followed quite many tutorials, in 2D and 3D physics, but overall I’ve been disappointed in the results. Not even one talked about braking in a “realistic” way. By realistic, I mean “likely”, obviously I’m not expecting any simulation with crazy equations.
So that leads us to my first code.
It’s a mix of things I learned here and there, and a hand-crafted part for the brakes that is probably going to scare some.
It’s based on my own logic (and ignorance in C Sharp) as everything I tried before having that working didn’t want to work. Lack of knowledge, undoubtedly.
It works, I’m not satisfied at all, but that’s all I could come up less than 2 weeks after having begun.
I’m not asking you to give me any tips if you don’t want to
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class CarController2DPhysics : MonoBehaviour
{
[SerializeField]
private Vector3 _centerOfMass;
public float speed = 4000;
public float rotationSpeed = 4000f;
public WheelJoint2D rearWheel;
public WheelJoint2D frontWheel;
private Rigidbody2D rb;
private float movement = 0f;
private float rotation = 0f;
private float brake = 0f;
private float nospeed = 0.1f;
private bool moveForward;
private bool moveBackward;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.centerOfMass = _centerOfMass;
}
void Update()
{
movement = -Input.GetAxisRaw("Vertical") * speed ;
brake = -Input.GetAxisRaw("Vertical") * nospeed;
rotation = Input.GetAxisRaw("Horizontal");
moveForward = (Input.GetKey(KeyCode.UpArrow));
moveBackward = (Input.GetKey(KeyCode.DownArrow));
}
void FixedUpdate()
{
rb.AddTorque(-rotation * rotationSpeed * Time.fixedDeltaTime);
if (movement == 0f)
{
//rearWheel.useMotor = false;
frontWheel.useMotor = false;
}
//}
else if (moveForward)
{
//rearWheel.useMotor = true;
frontWheel.useMotor = true;
JointMotor2D motor = new JointMotor2D { motorSpeed = movement , maxMotorTorque = frontWheel.motor.maxMotorTorque };
//rearWheel.motor = motor;
frontWheel.motor = motor;
}
if (moveBackward)
{
if (GetComponent<Rigidbody2D>().velocity.x > 0.5)
{
frontWheel.useMotor = true;
JointMotor2D motor = new JointMotor2D { motorSpeed = brake, maxMotorTorque = frontWheel.motor.maxMotorTorque };
frontWheel.motor = motor;
}
else if (GetComponent<Rigidbody2D>().velocity.x < 1)
{
frontWheel.useMotor = true;
JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = frontWheel.motor.maxMotorTorque };
frontWheel.motor = motor;
}
}
if (moveForward)
{
if (GetComponent<Rigidbody2D>().velocity.x < -0.5)
{
frontWheel.useMotor = true;
JointMotor2D motor = new JointMotor2D { motorSpeed = brake, maxMotorTorque = frontWheel.motor.maxMotorTorque };
frontWheel.motor = motor;
}
else if (GetComponent<Rigidbody2D>().velocity.x > -1)
{
frontWheel.useMotor = true;
JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = frontWheel.motor.maxMotorTorque };
frontWheel.motor = motor;
}
}
}
}
And finally, a video of my in-game test I made before starting that thread (I’ve been focusing on my models and mainly the textures for the past 2 weeks):
I hope you’ll enjoy that thread, I’ll do my best to make it pleasant and share many interesting things.