Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

MOVEMENT ANIMATION OF CAR IN UNITY

A topic by SOUVIK1508 created May 07, 2021 Views: 1,042
Viewing posts 1 to 1

We are a student of  VIT BHOPAL

This is a group activity given by our teacher.

Name : SOUVIK MODAK 

Reg . No : 20BCG10034

NAME : TANMAY MEHRA

REG NO: 20BCG10080

 So I’m going to show how to make a make a moving object(car) move in unity.

So first we have to add a moving object (car) in unity.........

I have added a car like this 


we can also get this on  asset store if we want (i.e, 3d low poly car).

Now, we have to create a script for the moving object (CAR) to make it move.



code:

using System;  
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
public class CarController : MonoBehaviour 
 { 
   private float horizontalInput;   
   private float verticalInput;   
   private float steerAngle;
   Private bool isBreaking; 
 
   public WheelCollider frontLeftWheelCollider; 
   public WheelCollider frontRightWheelCollider; 
   public WheelCollider rearLeftWheelCollider; 
   public WheelCollider rearRightWheelCollider; 
   public Transform frontLeftWheelTransform; 
   public Transform frontRightWheelTransform; 
   public Transform rearLeftWheelTransform; 
   public Transform rearRightWheelTransform; 
 
   public float maxSteeringAngle = 30f; 
   public float motorForce = 50f; 
   public float brakeForce = 0f; 
 
 
   private void FixedUpdate() 
   { 
     GetInput(); 
     HandleMotor(); 
     HandleSteering(); 
     UpdateWheels(); 
   } 
 
   private void GetInput() 
   { 
     horizontalInput = Input.GetAxis("Horizontal"); 
     verticalInput = Input.GetAxis("Vertical"); 
     isBreaking = Input.GetKey(KeyCode.Space); 
   } 
 
   private void HandleSteering() 
   { 
     steerAngle = maxSteeringAngle * horizontalInput; 
     frontLeftWheelCollider.steerAngle = steerAngle; 
     frontRightWheelCollider.steerAngle = steerAngle; 
   } 
 
   private void HandleMotor() 
   { 
      frontLeftWheelCollider.motorTorque = verticalInput * motorForce; 
      frontRightWheelCollider.motorTorque = verticalInput * motorForce; 
 
      brakeForce = isBreaking ? 3000f : 0f; 
      frontLeftWheelCollider.brakeTorque = brakeForce; 
      frontRightWheelCollider.brakeTorque = brakeForce; 
      rearLeftWheelCollider.brakeTorque = brakeForce; 
      rearRightWheelCollider.brakeTorque = brakeForce; 
   } 
 
   private void UpdateWheels() 
   { 
      UpdateWheelPos(frontLeftWheelCollider, frontLeftWheelTransform); 
      UpdateWheelPos(frontRightWheelCollider, frontRightWheelTransform); 
      UpdateWheelPos(rearLeftWheelCollider, rearLeftWheelTransform); 
      UpdateWheelPos(rearRightWheelCollider, rearRightWheelTransform); 
   } 
 
   private void  UpdateWheelPos(WheelCollider wheelCollider, Transform trans) 
   { 
      Vector3 pos; 
      Quaternion rot; 
      wheelCollider.GetWorldPose(out pos, out rot); 
      trans.rotation = rot; 
      trans.position = pos; 
   } 
 
} 

  So the above script will allow our car to move up,down,left,right and this is the way we can move the car in unity.

Below we have given some screenshots and videos of our project showing the movement animation of car:





video:


 Here are some references and source where you can download and find the material:

https://docs.unity.cn/560/Documentation/Manual/WheelColliderTutorial.html

https://assetstore.unity.com/packages/3d/vehicles/land/3d-low-poly-car-for-games...

https://docs.unity.cn/560/Documentation/Manual/class-WheelJoint2D.html


THANK YOU