Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question [Solved] Car cannot moving

Discussion in 'Physics' started by CreeperSaviour, Jul 7, 2023.

  1. CreeperSaviour

    CreeperSaviour

    Joined:
    Apr 16, 2023
    Posts:
    17
    A simple car was created with Unity.

    upload_2023-7-7_14-9-18.png

    upload_2023-7-7_14-9-46.png

    upload_2023-7-7_14-9-56.png

    upload_2023-7-7_14-10-9.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BackHoeController : MonoBehaviour {
    6.  
    7.     public List<AxleInfo> axleInfos;
    8.     public float maxMotorTorque;
    9.     public float maxSteeringAngle;
    10.  
    11.     public void ApplyLocalPositionToVisuals(WheelCollider collider)
    12.     {
    13.         if (collider.transform.childCount == 0) {
    14.             return;
    15.         }
    16.  
    17.         Transform visualWheel = collider.transform.GetChild (0);
    18.  
    19.         Vector3 position;
    20.         Quaternion rotation;
    21.         collider.GetWorldPose (out position, out rotation);
    22.  
    23.  
    24.         visualWheel.transform.position = position;
    25.         visualWheel.transform.rotation = rotation * Quaternion.Euler (0f, 0f, 90f);
    26.     }
    27.  
    28.     void Start () {
    29.         Debug.Log ("CarStart");
    30.     }
    31.    
    32.     void Update () {
    33.        
    34.     }
    35.  
    36.     public void FixedUpdate()
    37.     {
    38.         float motor = maxMotorTorque * Input.GetAxis("Vertical");
    39.         float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
    40.         Debug.Log ("CarFUpdate motor=" + motor + "steering=" + steering);
    41.  
    42.         foreach (AxleInfo axleInfo in axleInfos) {
    43.             if (axleInfo.steering) {
    44.                 axleInfo.leftWheel.steerAngle = steering;
    45.                 axleInfo.rightWheel.steerAngle = steering;
    46.             }
    47.             if (axleInfo.motor) {
    48.                 axleInfo.leftWheel.motorTorque = motor;
    49.                 axleInfo.rightWheel.motorTorque = motor;
    50.             }
    51.             ApplyLocalPositionToVisuals(axleInfo.leftWheel);
    52.             ApplyLocalPositionToVisuals(axleInfo.rightWheel);
    53.  
    54.         }
    55.     }
    56.  
    57. }
    58.  
    59. [System.Serializable]
    60.     public class AxleInfo {
    61.     public WheelCollider leftWheel;
    62.     public WheelCollider rightWheel;
    63.     public bool motor;
    64.     public bool steering;
    65. }
    66.  
    However, I am having trouble with it, because when I operate it right after it starts, it only moves a little, and when I operate it some time after it starts, it doesn't move at all.
    Please let me know what information you need and I will post it.

    Thank you.
     
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
  3. driftnumata

    driftnumata

    Joined:
    May 31, 2023
    Posts:
    29
    Did you check the "AxleInfo"'s motor and steer in your good locations?
    I have a simple tutorial video.
     
  4. CreeperSaviour

    CreeperSaviour

    Joined:
    Apr 16, 2023
    Posts:
    17
    Sorry for the much delayed reply.

    I am writing to you from this site (https://digilab.tech/%E3%80%90unity...87%AA%E5%8B%95%E8%BB%8A%E3%82%92%E4%BD%9C%E6% 88%90%E3%81%99%E3%82%8B/) and created it but apparently this site refers to the site you presented.
    Thanks to you, I followed that tutorial and it worked fine. Thank you very much.
     
    KillDashNine likes this.
  5. CreeperSaviour

    CreeperSaviour

    Joined:
    Apr 16, 2023
    Posts:
    17
    Like #2, sorry for the much delayed reply.

    In reply to #2, I got it to work for now, but I have only actually run the tutorial and have not caught up with my understanding. I am not good at English and could not find these videos. I think I need to watch these videos to gain more knowledge and fully understand why it didn't work. Thank you for the valuable information.
     
    driftnumata likes this.