Waypoint on Cars and Wheel Movements



  • Hello,

    I am trying to use Simple Waypoint System to create a simple traffic system with cars.

    It work like a charm but i am stuck with the Wheel movement.

    First i've set up nicely a car (separate Wheel colliders notably). Then I've taken a script online (from Car AI tutorial on YouTube) to have the Wheel collider point at each waypoint. I've made some VERY light changes hoping this would work with SWS but i've failed to far.

    My wheels simply turn left at runtime and then are stuck on the left position.

    Secondly i can't figure out how to have the Wheel spine on movement. It seems this can only be achieved using Wheel colliders and using the MotorTorque from script (which defy the purpose of your nice SplineMovement script).

    Any help would be most appreciated.

    BTW - I am very green when it comes to script and English is not my native langage so please be forgiving :)

    Many thanks

    Labarriere

    The code I am using to have the Wheel pointing at each subsequent waypoint.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CarEngine : MonoBehaviour
    {
        public Transform path;
        public float maxSteerAngle = 30f;
        public WheelCollider wheelFL;
        public WheelCollider wheelFR;
    
        private List<Transform> waypoints;
        private int currentWaypoint = 0;
    
        void Start() {
    
            Transform[] pathTransforms = path.GetComponentsInChildren<Transform>();
            waypoints = new List<Transform>();
    
            for (int i = 0; i < pathTransforms.Length; i++) {
                if (pathTransforms[i] != path.transform) {
                    waypoints.Add(pathTransforms[i]);
                }
            }
        }
    
        void FixedUpdate()
        {
            ApplySteer();
        }
    
        void ApplySteer() {
            Vector3 relativeVector = transform.InverseTransformPoint(waypoints[currentWaypoint].position);
            float newSteer = (relativeVector.x / relativeVector.magnitude) * maxSteerAngle;
            wheelFL.steerAngle = newSteer;
            wheelFR.steerAngle = newSteer;
        }
    }
    


  • Hey there, thanks for joining the forum.

    I think you are overcomplicating things a bit regarding the wheel direction. For example if it's just about the rotating, you could simply rotate the wheel in the direction of the next waypoint with this pre-built script, where you only need to set the target (waypoint) value:
    Smooth LookAt

    If that's not what you're looking for, the current direction value is also calculated for animations already, in the MoveAnimator script. Now, cars are not animators... :) but you can have a look at the humanoid walkers in the sample scenes, and open their Animator view. The direction value is used for turning animations, so humanoids can "lean" into curves. The MoveAnimator script calculates another value: speed. With speed, the correct idle/walk/run animation is played back. Why am I telling you this?

    Because both of these values actually provide want you are looking for:

    • direction, for rotating the wheel sideways
    • speed, for rotating the wheel forwards

    The implementation of this would be up to you, but the input variables exist in there :)


Log in to reply