However, the instantiated hovercraft behaves different from the one I place with the Editor. The Hovercraft / car that is instantiated drives like 5 times faster than it’s original. The hovercraft prefab is a complex object, holding a rigidbody and several child objects, some with wheelcolliders. But that shouldn’t be a problem, right?
the only thing that changes is that with instantiated objects, they don’t check for other objects instantiated at the same time anymore, so they don’t wait for all others to call awake / start before they go to the next step
Sounds like the prefab issue is a red herring. If something physical is behaving faster/slower under some circumstances, you should make sure you’re only applying forces in FixedUpdate and not Update, and anything you are doing in Update for other reasons is properly taking Time.deltaTime into account.
I thought about that too Matthew; it has to be something else. But fact remains; when I add the prefab manually through the editor it behaves differently from when I add it through the above code. So there must be a difference in the two methods.
About your suggestion; I walked through all the code many times to check such a thing. Changed some of the update() methods to fixedUpdate() where there was any doubt. It doesnt change anything however. There is also no force I add or values I increase on update-like methods. All that happends is the change (not increase; change) of the motortorque, wheelangle, and properties like that of the wheelcolliders. I debugged them, they are all the same on both objects…
This may not be much help with your current situation, but I’ve had random problems when instantiating wheelcolliders from code. I ended up just making sure the wheelcolliders were present in my scene from the start instead of instantiating them from code. I have spoken to at least one other person who also had problems with code-instantiated wheelcolliders.
The problems weren’t that things moved extra fast though, it was more like they didn’t collide properly with items in the scene, and collided with other items that weren’t there at all. I couldn’t make sense of what they were doing enough to find any logic to it.
Since then I’ve had a few other strange and seemingly unsolvable problems with wheelcolliders, so in the end I decided to switch to a self-coded raycast wheel solution.
duck, thanks for your reply. I’ve had troubles with wheel colliders as well. They’re just buggy things it seems.
Making sure the objects are already in the scene is an awfull solution, but it crossed my mind also.
I think I isolated the bug - because a bug it is clearly.
I built up a car from very simple towards the car I already have, to see where it goes wrong. The problem is the car is made out of more than 1 gameobject, it has childs with childs, and the wheelcollider doesnt seem to like that.
This goes alright:
Main object (mesh, rigidbody, boxcollider)
–child object (wheelcollider)
–child object (wheelcollider)
–child object (wheelcollider)
–child object (wheelcollider)
I just posted a question and reading your problem, i see similarities; I have a pre-fab with nested game objects, and the original works fine, but once I instance multiple ones, only one of the works, the rest are dead. May be related, but make sure that the transforms for the game objects you use to build your hierarchy have a neutral (0 translation, 0 rotation, 1 scale). Somehow, when first creating a game object, it has different values for its transform and when dragged into a hierarchy, it keeps such numbers. That alone caused me quite a few headaches.
First an update; Yesterday, I worked on the testcar, making it my final car step by step, see what goes wrong. I ended up with the final car with no problems. It just spawned and drived normally. Then I make a build, run it… car drives 5 times faster (whaat!). I restart the editor, car runs faster in the editor now as well. :x
Today I’m gonna repeat it and make builds everytime.
Andee, which script are you refering to? I instantiate with the code above. The code to move the car is this:
using UnityEngine;
using System.Collections;
public class scrTestCar : MonoBehaviour
{
public float fSpeedFactor = 180.0f;
public float fReverseSpeed = 150.0f;
public float fBrakeFactor = 180.0f;
public float fSteerFactor = 20.0f;
Component[] wheelArray = null;
private scrInputManager inputManager = null;
private float fMotorPowerFrontRearRatio = 1f; // Devides the moter power over the wheels. 0 = only front wheel drive, 1 = only rear wheel drive.
private float fBrakePowerFrontRearRatio = 0.3f; // Devides the brake power over the wheels. 0 = only front wheel braking, 1 = only rear wheel braking.
void Awake()
{
Component componentTemp = GameObject.FindGameObjectWithTag("tagGameController").GetComponent("scrInputManager");
if (componentTemp is scrInputManager)
inputManager = (scrInputManager)componentTemp;
else
Debug.Log("ScrHovercraftController could not find scrInputManager");
// Gather all wheels in an array
wheelArray = transform.GetComponentsInChildren(typeof(WheelCollider));
rigidbody.centerOfMass = new Vector3(0, -0.32f, 0.25f);
}
void Update()
{
float fVehicleSpeed = Vector3.Dot(rigidbody.velocity, transform.forward);
float fMotorPower = inputManager.BikeSpeed * fSpeedFactor;
float fSteerAngle = inputManager.SteerDirection * fSteerFactor;
if (wheelArray.Length > 0)
{
// Loop trough the wheels in the array
foreach (Component currentComponent in wheelArray)
{
// Cast each wheel to a proper WheelCollider object
WheelCollider currentWheel = (WheelCollider)currentComponent;
// If this is a front wheel
if (currentWheel.transform.localPosition.z > 0)
{
// Apply steering
if (inputManager.SteerDirection != 0)
currentWheel.steerAngle = fSteerAngle;
// Apply motorpower
if (inputManager.BikeSpeed > 0)
currentWheel.motorTorque = fMotorPower * (1 - fMotorPowerFrontRearRatio);
else
currentWheel.motorTorque = 0;
// Apply brakePower
if (inputManager.BrakePower > 0 fVehicleSpeed >= 0.1)
currentWheel.brakeTorque = inputManager.BrakePower * fBrakeFactor * (1 - fBrakePowerFrontRearRatio);
else
currentWheel.brakeTorque = 0;
}
// Rear wheel
else
{
// Apply motorpower
if (inputManager.BikeSpeed > 0)
currentWheel.motorTorque = fMotorPower * fMotorPowerFrontRearRatio;
else
currentWheel.motorTorque = 0;
// Apply brakepower
if (inputManager.BrakePower > 0)
{
// When not in motion; apply reversed motorpower
if (fVehicleSpeed < 0.1)
{
currentWheel.brakeTorque = 0;
currentWheel.motorTorque = inputManager.BrakePower * fReverseSpeed * -1;
}
// Else, apply brakepower
else
currentWheel.brakeTorque = inputManager.BrakePower * fBrakeFactor * fBrakePowerFrontRearRatio;
}
else
currentWheel.brakeTorque = 0;
}
}
}
else
Debug.Log("scrHovercraft: No Wheels found");
// Reset car
if (inputManager.ResetCar)
{
Quaternion quaTemp = Quaternion.Euler(new Vector3(0, gameObject.transform.rotation.eulerAngles.y, 0));
gameObject.transform.rotation = quaTemp;
gameObject.transform.Translate(new Vector3(0, 1, 0));
}
}
}
Zorba, might be the same. The problem I have is very related to the physics though. I blame the wheelcollider, as I’ve had some troubles with colliders and nested gameobjects earlier (http://forum.unity3d.com/viewtopic.php?t=33249).
I am aware of the transform values being changed or set to rubbish when dragging a prefab to the scene, but thanks for the suggestion!
Well, if anyone is still interested in this, or for the people that might run into this as well; I found a work-around
I create the complete car out of code and that seems to work. Creating only the wheelcolliders in code, for objects that are already made in the editor, doesnt work as duck pointed out. The wheelcolliders go totally crazy. But building the entire car from code does work and there is no difference anymore between instantiated and dragged into the scene ones.
Guys! I know it’s a very old post but it seems that this problem still persists in Unity 3.3.0f4.
I have the exact same issue: manually placing an instance of a car prefab in the editor allows the car to work perfectly, but as soon as I use Instantiate to create an instance of that car the car behaves very strange! It’s not moving straight, it’s getting pulled in random direction everytime I click Play in the editor.
Unity team, please confirm the presence of this issue. I am 100% it’s not something wrong with my scripts. The car works great everytime if it’s manually placed in the scene in Edit Mode.
I tried manually adding the wheel colliders from code too, but the same thing happens.
My car hierarchy is as follows:
* Car (parent GameObject with rigidbody and main control script)
-> ChassisCollider (child GameObject with BoxCollider)
-> Graphic (child GameObject with MeshRenderer)
-> WheelColliders (child GameObject with translate 0, scale 1, rotation 0)
-> Wheel_FL (child GameObject with WheelCollider)
-> Wheel_FR (child GameObject with WheelCollider)
-> Wheel_RL (child GameObject with WheelCollider)
-> Wheel_RR (child GameObject with WheelCollider)
Anyone managed to find out why this issue still happens in v3.3.0f4 ? Is it a bug in Unity?