Unity Caiji self-study record 3

Unity self-study record 3

Vehicle creation
1. Create an empty object as the root node of the car and name it car_root.
2. Mount a rigid body component for car_root and adjust the mass to 1500 (kg) to simulate the attributes of the real car.
3. Car body creation: create a cube and adjust Size, let it be a child of car_root, reset the position,
here suggest the size attribute cube (3, 5, 3).
4. Add the car wheel root node: create a child of car_root (empty object), rename is "wheel" , Reset the wheel position, create 4 (n) sub-objects (specific wheels) for the wheel, 4 reels to create 4, you can also create a copy of n if it is too much trouble. After creating the root node of the wheel, mount physics->Wheel Collider for it. Adjust the position. Recommended data here (1, -1, 1) (1, -1, -1) (-1, -1, 1) (-1) , -1, -1).
Controller code After
creating the basic car shape, create a C# controller script to control the wheels and car activities.

using UnityEngine;
using system.Collections;
using System.Collections.Generic;

public class SimpleCarController : MonoBehaviour {
    
    
public List<AxleInfo> axleInfos;
public float maxMotorTorque;
public float maxSteeringAngle;

public void FixedUpdate()
{
    
    
	float motor = maxMotorTorque * Input.GetAxis("Vertical");
	float steering = maxSteeringAngle * Input. GetAxis("Horizontal");
foreach (AxleInfo axieInfo in axleinfos) {
    
    
	if (axleInfo>steering) {
    
    
		axleInfo.leftWheel.steerAngle = steering;
		axleInfo.rightWheel.steerAngle = steering;
		}
		if (axleInfo.motor) {
    
    
			axleInfo.lefrWheel.motorTorque = motor;
			axleInfo.rightWheel.motorTorque = motor;
				}
			}
		}
	}
[System.Serializable]
[System.Serializable]
public WheelCollider leftwheel;
public WheelCollider rihtWheel;
public bool motor;
public bool steering;
}

"2" Tumbler picking object creation
(1) Import the model into the scene, create an empty object for the model as the root node, and the model as the child node of the root node.
(2) Mount a rigid body to the root node. After the rigid body is mounted, it is controlled by gravity, so the mass is set to be less than 1 (kg). In addition, angular damping is also very important, which mainly affects the tumbler shaking frequency.
(3) Mount the collision body (still according to the fitting contour criterion, choose when the basic collision body shape)
(4)! Add script control center of gravity.

public class Tumbler :Monobehaviour {
    
    
	Rigidbody rigid;
void Stard () {
    
    
rigid = GetComponent<Rigidbody>();
rigid.centerOfMass = new Vector3(0,-1,0);	//y值越小(负数)即重心越低,越稳定
	}
}

After finishing, adjust it in the scence window to offset the initial position, adjust the offset, and then start in the game window, you will get a fun tumbler haha~

Share current plug-established track racing game with the best krypton gold, like the free version of E *** R *** 3D (protected by copyright mosaic) plug-in, go to the map may be lost. Moreover, it is not very convenient to adjust the nodes in terms of road establishment. Although it is very powerful to fit the terrain and view the flying and surrounding roads, the road display is not very good and it feels that it overlaps with the terrain and keeps flashing.

Guess you like

Origin blog.csdn.net/Virgile_J/article/details/103341512