Wednesday, 11 June 2014

# Free - NGUI Steering Wheel in C# for Unity3D Car Games (NGUI ile donen direksiyon kodu)


Hi all again, I was getting into car games but I needed a detailed, easy to use steering wheel. It had to be in NGUI and C#.

I have checked out all internet, and found one from "Yasir Kula", he nicely shared his code. But it was in Javascript and it designed to work with stock Unity3D gui.

So here, I have optimized his code, added some features, extremely easy to use and works nicely with NGUI, C# and EDY VEHICLE PHYSICS.

It is free to use,  at least you can thank me :)

-----------------------------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections;

// Attach this to NGUI UIButton (Steering Wheel)
// Adapted from Yasir Kula
// AuthorOnur Mete Kaplan
public class CarSteeringWheel : MonoBehaviour {
    
    // NGUI UI Root camera (Not any other game play camera)
    public Camera camera;

    // Changable Data
    public float wheelAngleMax = 500.0f// 500 is an ideal number
    public float speedRotateBack = 1000.0f// Rotate back hydrolic speed

    // Rotation angle for EDY VEHICLE PHYSICS [Angle -1.0 to 1.0]
    public float wheelAngleScaledToOne = 0.0f;

    // Widget data
    private UIWidget uiWidget;

    // Wheel data
    private Vector3 wheelCenter;
    private float wheelAngle;
    private float wheelOldAngle;
    private bool isReset = false;
    
    // Touch data
    private bool isPressed = false;

    void Start () {

        // Get this widget
        uiWidget = this.GetComponent<UIWidget>() as UIWidget;
    
        // Get widget center
        wheelCenter = camera.WorldToScreenPoint(uiWidget.worldCenter);
        log("Widget Center: " + wheelCenter.ToString());
    }

    void Update(){

        // If user do not touch reset wheel for once
        if(!isPressed && !isReset){
            resetWheel();
        } 
    }
    
    void OnPress (bool isPressed){ 

        // Pressed to wheel
        this.isPressed = isPressed;

        // First touch to wheel
        wheelOldAngle = Vector2.Angle(Vector2.upnew Vector3(UICamera.lastTouchPosition.x - wheelCenter.xUICamera.lastTouchPosition.y - wheelCenter.y0.0f));
        
        // Not reset
        isReset = false;
    }
    
    void OnDrag(Vector2 delta){

        // Steer the wheel
        if(isPressed){

            // Do steering action
            doSteeringCalculations();
            doSteeringWidgetRotation();
        } 
    }

    private void doSteeringCalculations(){

        // Find new angle
        float wheelNewAngle = Vector2.Angle(Vector2.upnew Vector3(UICamera.lastTouchPosition.x - wheelCenter.xUICamera.lastTouchPosition.y - wheelCenter.y0.0f));

        // Touching very close to widget centerskip
        ifVector2.Distance(new Vector2(UICamera.lastTouchPosition.x,UICamera.lastTouchPosition.y), new Vector2(wheelCenter.xwheelCenter.y)) > 10f ){

            // Find final rotate angle
            if(UICamera.lastTouchPosition.x > wheelCenter.x){
                wheelAngle -= wheelNewAngle - wheelOldAngle;
            } else{
                wheelAngle += wheelNewAngle - wheelOldAngle;
            }
        }

        // Maximum angle (last check)
        ifwheelAngle > wheelAngleMax ){
            wheelAngle = wheelAngleMax;
        } else ifwheelAngle < -wheelAngleMax ){
            wheelAngle = -wheelAngleMax;
        }

        // Update angle
        wheelOldAngle = wheelNewAngle;

        // Log angle
        log("Wheel Angle: " + wheelAngle);
    }

    private void doSteeringWidgetRotation(){

        // Rotate the widget view
        this.transform.rotation = Quaternion.Euler(0f,0f,wheelAngle);

        // This is extra for EDY VEHICLE PHYSICS [Angle -1.0 to 1.0]
        if(wheelAngle != 0.0f){
            wheelAngleScaledToOne = -wheelAngle / wheelAngleMax;
        }else{
            wheelAngleScaledToOne = 0.0f;
        }
        log ("Wheel Angle Scale To One: " + wheelAngleScaledToOne);
    }

    private void resetWheel(){

        // if not resetted
        if(!Mathf.Approximately(0fwheelAngle )){

            // Log reset
            log ("Resetting wheel angle: " + wheelAngle);

            // Do widget view rotation
            doSteeringWidgetRotation();

            // Delta angle
            float deltaAngle = speedRotateBack * Time.deltaTime;

            // Check if is almost resetted
            ifMathf.AbsdeltaAngle ) > Mathf.AbswheelAngle ) ){
                wheelAngle = 0f;
                return;
            }
        
            // Continue reseting
            ifwheelAngle > 0f ){
                wheelAngle -= deltaAngle;
            }else{
                wheelAngle += deltaAngle;
            }

        } else{

            // Reset
            wheelAngle = 0.0f;
            wheelAngleScaledToOne = 0.0f;
            doSteeringWidgetRotation(); // Last check for %100 0.0f
            isReset = true;
        }
    }

    public void reset(){

        // Reset
        wheelAngleScaledToOne = 0.0f;
        wheelAngle = 0.0f;
        wheelOldAngle = 0.0f;
        isReset = false;
        isPressed = false;
    }

    private void log(string info){
        // Debug.Log("SteeringWheel " + info);
    }
}

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. What a great sort of the information regarding to the games development and also for the window platform. i played the flash games but the window games are of the great.
    Makeover Games

    ReplyDelete