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

Other HitTest UnityCar Unity 2020.3.48f1

Discussion in 'Scripting' started by mdj12, Sep 8, 2023.

  1. mdj12

    mdj12

    Joined:
    Nov 10, 2021
    Posts:
    12
    So I develop a game where I using UnityCar Pro 2.1 as vehicle controller (I know is deprecated and old but is one of prettiest vehicle controller and it also a project for fixing UnityCar for working on newer versions of unity, and I also didn't want to use any other CarController) I fixed 90% of it but I get an error from MobileCarController (which I didn't use but some scripts requies it) "'Image' does not contain a definition for 'HitTest' and no accessible extension method 'HitTest' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)" Here is the script
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MobileCarController : CarController{
    4.     //-------------------------------------------------------------------------
    5.     // Public class definition
    6.     //-------------------------------------------------------------------------
    7.     [System.Serializable]
    8.     public class TouchButton{
    9.         public UnityEngine.UI.Image texture = null;
    10.         public float alphaButtonDown = 0.20f;
    11.         public float alphaButtonReleased = 0.30f;
    12.  
    13.     }
    14.    
    15.     //-------------------------------------------------------------------------
    16.     // Public class properties
    17.     //-------------------------------------------------------------------------
    18.     public TouchButton throttleButton;
    19.     public TouchButton brakeButton;
    20.     public TouchButton handbrakeButton;
    21.     public TouchButton gearUpButton;
    22.     public TouchButton gearDownButton;
    23.     public TouchButton learnButton;
    24.     public TouchButton handLearnButton;
    25.    
    26.    
    27.     //-------------------------------------------------------------------------
    28.     // Override Methods
    29.     //-------------------------------------------------------------------------
    30.     protected override void GetInput(out float throttleInput,
    31.                                     out float brakeInput,
    32.                                     out float steerInput,
    33.                                     out float handbrakeInput,
    34.                                     out float clutchInput,
    35.                                     out bool startEngineInput,
    36.                                     out int targetGear){
    37.        
    38.         clutchInput=0;
    39.         startEngineInput=false;
    40.        
    41.         // Read the steering from accelerometers
    42.         steerInput = Mathf.Clamp(-Input.acceleration.y, -1, 1);
    43.        
    44.         // Read the throttle, brake and handbrake input from touch buttons
    45.         throttleInput = 0f;
    46.         brakeInput = 0f;
    47.         handbrakeInput = 0f;
    48.         bool gearUp = false;
    49.         bool gearDown = false;
    50.  
    51.        
    52.         // Iterate over all touches...
    53.         int touchCount = Input.touchCount;
    54.         for (int i = 0; i < touchCount; ++i){
    55.             Touch t = Input.GetTouch(i);
    56.  
    57.             //From here the error starts (Any other comment it's from the script itself
    58.             // Check if the throttle button is pressed
    59.             if (t.phase != TouchPhase.Ended &&
    60.                 this.throttleButton != null &&
    61.                 this.throttleButton.texture != null &&
    62.                 this.throttleButton.texture.HitTest(t.position)){
    63.                
    64.                 throttleInput = 1f;
    65.                
    66.                 Color c = this.throttleButton.texture.color;
    67.                 c.a = this.throttleButton.alphaButtonDown;
    68.                 this.throttleButton.texture.color = c;
    69.             }
    70.            
    71.             // Check if the brake button is pressed
    72.             if (t.phase != TouchPhase.Ended &&
    73.                 this.brakeButton != null &&
    74.                 this.brakeButton.texture != null &&
    75.                 this.brakeButton.texture.HitTest(t.position)){
    76.                
    77.                 brakeInput = 1f;
    78.                
    79.                 Color c = this.brakeButton.texture.color;
    80.                 c.a = this.brakeButton.alphaButtonDown;
    81.                 this.brakeButton.texture.color = c;
    82.             }
    83.            
    84.             // Check if the handbrake button is pressed
    85.             if (t.phase != TouchPhase.Ended &&
    86.                 this.handbrakeButton != null &&
    87.                 this.handbrakeButton.texture != null &&
    88.                 this.handbrakeButton.texture.HitTest(t.position)){
    89.                
    90.                 handbrakeInput = 1f;
    91.                
    92.                 Color c = this.handbrakeButton.texture.color;
    93.                 c.a = this.handbrakeButton.alphaButtonDown;
    94.                 this.handbrakeButton.texture.color = c;
    95.             }
    96.            
    97.             // Check if the "gear up" button has been pressed this frame
    98.             if (t.phase == TouchPhase.Began &&
    99.                 this.gearUpButton != null &&
    100.                 this.gearUpButton.texture != null &&
    101.                 this.gearUpButton.texture.HitTest(t.position)){
    102.                
    103.                 gearUp = true;
    104.                
    105.                 Color c = this.gearUpButton.texture.color;
    106.                 c.a = this.gearUpButton.alphaButtonDown;
    107.                 this.gearUpButton.texture.color = c;
    108.             }
    109.            
    110.             // Check if the "gear down" button has been pressed this frame
    111.             if (t.phase == TouchPhase.Began &&
    112.                 this.gearDownButton != null &&
    113.                 this.gearDownButton.texture != null &&
    114.                 this.gearDownButton.texture.HitTest(t.position)){
    115.                
    116.                 gearDown = true;
    117.                
    118.                 Color c = this.gearDownButton.texture.color;
    119.                 c.a = this.gearDownButton.alphaButtonDown;
    120.                 this.gearDownButton.texture.color = c;
    121.             }
    122.         }
    123.        
    124.         // Change gear if necessary
    125.         targetGear = drivetrain.gear;
    126.         if (gearUp && !gearDown){
    127.             ++targetGear;
    128.         }else if (gearDown && !gearUp){
    129.             --targetGear;
    130.         }
    131.        
    132.         // Finally, change the button alphas
    133.         if (this.throttleButton != null &&
    134.             this.throttleButton.texture != null &&
    135.             throttleInput < Mathf.Epsilon){
    136.            
    137.             Color c = this.throttleButton.texture.color;
    138.             c.a = this.throttleButton.alphaButtonReleased;
    139.             this.throttleButton.texture.color = c;
    140.         }
    141.        
    142.         if (this.brakeButton != null &&
    143.             this.brakeButton.texture != null &&
    144.             brakeInput < Mathf.Epsilon){
    145.            
    146.             Color c = this.brakeButton.texture.color;
    147.             c.a = this.brakeButton.alphaButtonReleased;
    148.             this.brakeButton.texture.color = c;
    149.         }
    150.        
    151.         if (this.handbrakeButton != null &&
    152.             this.handbrakeButton.texture != null &&
    153.             handbrakeInput < Mathf.Epsilon){
    154.            
    155.             Color c = this.handbrakeButton.texture.color;
    156.             c.a = this.handbrakeButton.alphaButtonReleased;
    157.             this.handbrakeButton.texture.color = c;
    158.         }
    159.        
    160.         if (this.gearUpButton != null &&
    161.             this.gearUpButton.texture != null &&
    162.             !gearUp){
    163.            
    164.             Color c = this.gearUpButton.texture.color;
    165.             c.a = this.gearUpButton.alphaButtonReleased;
    166.             this.gearUpButton.texture.color = c;
    167.         }
    168.        
    169.         if (this.gearDownButton != null &&
    170.             this.gearDownButton.texture != null &&
    171.             !gearDown){
    172.            
    173.             Color c = this.gearDownButton.texture.color;
    174.             c.a = this.gearDownButton.alphaButtonReleased;
    175.             this.gearDownButton.texture.color = c;
    176.         }
    177.     }
    178. }
    179.  
    Regards,
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,195
  3. mdj12

    mdj12

    Joined:
    Nov 10, 2021
    Posts:
    12
    It's not missing anything, it is the full package
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,195
    EDIT: simple googling for that error and for UnityEngine.UI.Image.HitTest() reveals that it is an old method no longer available. You probably want to stick with tutorials from the past year or so, especially when starting out. Software APIs such as Unity are never static: they grow and evolve constantly.
     
    Last edited: Nov 12, 2023
  5. mdj12

    mdj12

    Joined:
    Nov 10, 2021
    Posts:
    12
    Fixed by me (removed the class and it's references from other classes because I don't use it in my project).Moderator, close this theard.