Change swivel axis to script in Unity

I am trying to simulate a car turning with HingeJoint (or a custom joint) in a Unity game engine.

I have 4 wheels attached to the main body, with joints, which works well. Now I need to change the axis of the joints (relative to the main body) using the script controller to rotate the wheels. It does what I need if I change the Z axis value in the inspector (rotating the axis relative to the main body), but when I try to change the Z axis in the script, everything gets crazy. It seems to me that the axis is relative to the parent body before starting the game, but when you start it, the axis may become absolute or something.

I tried to find the answer for some time, but I could not find anything related to the axis.

The only thing I found is TargetRotation (on a custom joint), which is actually not what I am looking for, because I believe that you need to set some restrictions on the other axis to make it work, thereby making it already springy the joints still swell. Obviously, I need the result to be as tough as possible. However, if you have an idea how to do this with TargetRotation , let me know because I have not yet been able to recognize how this works.

I think the question is quite simple, but if you feel that the question is not clear enough, let me know, so I will add some photos for a better explanation.

EDIT: Well, adding more information, we suggested the following:

Here is what i have enter image description here

As you can see its simple box with 4 cylinders attached to a box with customizable points. All elements are on the same root level and have rigid attachments.

In red circles, u can see different axis rotations made by changing the values โ€‹โ€‹of the Z axis (from -1 to 1) in the inspector, which is exactly what I need to do with a script to drive a car.

Unfortunately, when I add the Z axis controller to the script as follows:

  float steering = Input.GetAxis("Horizontal"); configurableJoint.axis = new Vector3(configurableJoint.axis.x, configurableJoint.axis.y, steering); 

It breaks like this: enter image description here

So, I would like to know how such a function can be achieved, as well as someone who can explain to me how the axes of the joints will work.

Thanks in advance for any efforts.

+5
source share
1 answer

I assume that you will need to install the wheels on the car body, and then turn the wheels relative to the car body in a script.

This will help if you can provide a simple Unity project that can be downloaded, experimented and tuned to duplicate the problem and fix it. I am happy to help if you want to send a simple demonstration of the problem so that I can fix it.

0
source

Source: https://habr.com/ru/post/1238771/


All Articles