Search Unity

Simply hover car AI

Discussion in 'Scripting' started by moesboy, Sep 2, 2015.

  1. moesboy

    moesboy

    Joined:
    Jun 21, 2015
    Posts:
    2
    Hello!

    Somewhat new to unity and have hit the first road block in a simple hover ship racing game I am making in my spare time. I have a drive-able hover-ship (from a unity tutorial, needed something super simple) I cant figure out how to make a car controller/AI controller for the opponents.

    I have been able to follow some tutorials and make a wheeled vehicle and make it go around the track following an array but I am having trouble adopting it to the hover ship script. Anyone have any suggestions on how I should go about it?

    Here is the code for the players ship so far:
    Let me know if you need any other info.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class HoverMotor : MonoBehaviour {
    6.    
    7.     public float speed = 90f;
    8.     public float turnSpeed = 5f;
    9.     public float hoverForce = 65f;
    10.     public float hoverHeight = 3.5f;
    11.     private float powerInput;
    12.     private float turnInput;
    13.     private Rigidbody carRigidbody;
    14.  
    15.  
    16.    
    17.     void Awake ()
    18.     {
    19.         carRigidbody = GetComponent <Rigidbody>();
    20.     }
    21.    
    22.     void Update ()
    23.     {
    24.         powerInput = Mathf.Lerp (powerInput,Input.GetAxis ("Vertical"),Time.deltaTime*1);
    25.         turnInput = Input.GetAxis ("Horizontal");
    26.     }
    27.    
    28.     void FixedUpdate()
    29.     {
    30.         Ray ray = new Ray (transform.position, -transform.up);
    31.         RaycastHit hit;
    32.        
    33.         if (Physics.Raycast(ray, out hit, hoverHeight))
    34.         {
    35.             float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
    36.             Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
    37.             carRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
    38.         }
    39.        
    40.         carRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
    41.         carRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
    42.        
    43.     }
    44. }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Hover cars are different from cars, cars use their wheels while steering to stop tge forces. Hover cars don't have this, it would be harder to implement too. They stop these forces vy going diagonal at the second road.