- Home /
Problem with cars in Unity 5
This is pretty much my first time making something like this so please be easy, these questions also might be obvious, but i haven't been able to find answers. (all these questions are based around the same car, in the same project/scene).
.
Question #1: I am making my first game heavily inspired by Brackeys when he had to make a game based on the topic "Small World". It's basically a car that drives around a sphere in the sky right now, but i'm having a lot of problems already.
(Its not letting me upload an image so this a link to Pasteboard https://pasteboard.co/IUVWjsg.png) .
So in my scene there, if i start driving my car it goes very slowly and after i made the mass of the car go up from 100 to 1500, and the the wheels go up from 20 to 1000 it was actually driveable, but even after i did that it was still very slow, so i bumped it up even more, and it just stopped going forwards (btw im not able to scale anything down, don't ask). This question leads me on to my next one.
.
Question #2: So I tried driving around the very slow car and after i reached the bottom, it didn't want to move anymore, even though I have gravity attractor and body scripts, it just stopped, but it works again after i manually dragged it back up to the top of the sphere.
Gravity Attractor script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityAttractor : MonoBehaviour
{
public float gravity = -3700f;
public void Attract(Transform body)
{
Vector3 targetDir = (body.position - transform.position).normalized;
Vector3 bodyUp = body.up;
body.rotation = Quaternion.FromToRotation(bodyUp, targetDir) * body.rotation;
body.GetComponent<Rigidbody>().AddForce(targetDir * gravity);
}
}
Gravity Body script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (Rigidbody))]
public class GravityBody : MonoBehaviour
{
GravityAttractor planet;
void Awake()
{
planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate()
{
planet.Attract(transform);
}
}
.
Question #3 (Final): When I am turning my car it works but doesnt at the same time (the wheels turn and everything) but instead of actually turning, it just drifts of to the right or left and i would like to know how to fix that. Here is my script for the car (you will probably need it to answer the other questions as well. Full Car script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public void GetInput()
{
m_horizontalInput = Input.GetAxis("Horizontal");
m_verticalInput = Input.GetAxis("Vertical");
}
private void Steer()
{
m_steeringAngle = maxSteerAngle * m_horizontalInput;
wheelFRW.steerAngle = m_steeringAngle;
wheelFLW.steerAngle = m_steeringAngle;
}
private void Accelerate()
{
wheelFRW.motorTorque = m_verticalInput * motorForce;
wheelFLW.motorTorque = m_verticalInput * motorForce;
}
private void UpdateWheelPoses()
{
UpdateWheelPose(wheelFRW, wheelFRT);
UpdateWheelPose(wheelFLW, wheelFLT);
UpdateWheelPose(wheelBRW, wheelBRT);
UpdateWheelPose(wheelBLW, wheelBLT);
}
private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
{
Vector3 _pos = _transform.position;
Quaternion _quat = _transform.rotation;
_collider.GetWorldPose(out _pos, out _quat);
_quat = _quat * Quaternion.Euler(new Vector3(0, -90, 0));
print(_quat);
_transform.position = _pos;
_transform.rotation = _quat;
}
private void FixedUpdate()
{
GetInput();
Steer();
Accelerate();
UpdateWheelPoses();
}
private float m_horizontalInput;
private float m_verticalInput;
private float m_steeringAngle;
public WheelCollider wheelFRW, wheelFLW;
public WheelCollider wheelBRW, wheelBLW;
public Transform wheelFRT, wheelFLT;
public Transform wheelBRT, wheelBLT;
public float maxSteerAngle = 30;
public float motorForce = 50;
}
In-Game options that came out of the script:https://pasteboard.co/IUU9lhO.png .
I'm sorry if this was annoying, but i'm very stupid. I would love any answers for any of these questions if you could. Sorry about the pasteboard links, it wouldnt let me upload an image. (also ask me if there is any more pictures or information from my project you need to be able to help me) Thank You!
Your answer
Follow this Question
Related Questions
Problems with cars in Unity 5 0 Answers
How to automatically stop car when fuel ends up ? 2 Answers
Strange Wheel Colliders 0 Answers
Car Physics 0 Answers