UnityCar MobileCarController без акселерометра

Программирование на Юнити.

UnityCar MobileCarController без акселерометра

Сообщение Duka 22 апр 2014, 18:34

Хочу в UnityCar сделать повороты в MobileCarController кнопочными, без акселерометра.

Думал возьму часть кода из AxisCarController и поменяю в MobileCarController, но там как-то все совсем заморочено. Я вообще не понял откуда берутся все данные. :((
Где указано, к примеру, для AxisCarController, что при нажатии "W" - едем вперед. Я этого не нашел, - там только public string throttleAxis="Throttle";, потом throttleInput= Input.GetAxisRaw (throttleAxis); и все... а что это значит я не понимаю))) Кто шарит, помогите плиз!
Аватара пользователя
Duka
UNец
 
Сообщения: 15
Зарегистрирован: 20 мар 2014, 03:37
Откуда: Харьков, UA
Skype: Deftrax
  • Сайт
  • ICQ

Re: UnityCar MobileCarController без акселерометра

Сообщение OlegNick 23 апр 2014, 01:12

А вы ищите строки
If (Input.GetKey("w"))
Едем вперед();
Усложнять - просто, упрощать - сложно.
OlegNick
Старожил
 
Сообщения: 585
Зарегистрирован: 10 ноя 2013, 02:21
Откуда: Россия, Калуга

Re: UnityCar MobileCarController без акселерометра

Сообщение ASD 23 апр 2014, 16:26

OlegNick писал(а):А вы ищите строки
If (Input.GetKey("w"))
Едем вперед();


Не будет там такого. Потому как используется input.GetAxis. Дайте кусок кода побольше, так сложно сказать
SmartMove Games
ASD
UNIверсал
 
Сообщения: 400
Зарегистрирован: 14 дек 2012, 23:40
Откуда: Харьков
  • ICQ

Re: UnityCar MobileCarController без акселерометра

Сообщение Duka 23 апр 2014, 17:19

Вот:
Синтаксис:
Используется csharp
using UnityEngine;

public class MobileCarController : CarController{
        //-------------------------------------------------------------------------
        // Public class definition
        //-------------------------------------------------------------------------
        [System.Serializable]
        public class TouchButton{
                public GUITexture texture = null;
                public float alphaButtonDown = 0.20f;
                public float alphaButtonReleased = 0.30f;
        }
       
        //-------------------------------------------------------------------------
        // Public class properties
        //-------------------------------------------------------------------------
        public TouchButton throttleButton;
        public TouchButton brakeButton;
        public TouchButton handbrakeButton;
        public TouchButton gearUpButton;
        public TouchButton gearDownButton;
       
        //-------------------------------------------------------------------------
        // Override Methods
        //-------------------------------------------------------------------------
        protected override void GetInput(out float throttleInput,
                                                                        out float brakeInput,
                                                                        out float steerInput,
                                                                        out float handbrakeInput,
                                                                        out float clutchInput,
                                                                        out bool startEngineInput,
                                                                        out int targetGear){
               
                clutchInput=0;
                startEngineInput=false;
               
                // Read the steering from accelerometers
                steerInput = Mathf.Clamp(-Input.acceleration.y, -1, 1);
               
                // Read the throttle, brake and handbrake input from touch buttons
                throttleInput = 0f;
                brakeInput = 0f;
                handbrakeInput = 0f;
                bool gearUp = false;
                bool gearDown = false;
               
                // Iterate over all touches...
                int touchCount = Input.touchCount;
                for (int i = 0; i < touchCount; ++i){
                        Touch t = Input.GetTouch(i);
                       
                        // Check if the throttle button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.throttleButton != null &&
                            this.throttleButton.texture != null &&
                            this.throttleButton.texture.HitTest(t.position)){
                               
                                throttleInput = 1f;
                               
                                Color c = this.throttleButton.texture.color;
                                c.a = this.throttleButton.alphaButtonDown;
                                this.throttleButton.texture.color = c;
                        }
                       
                        // Check if the brake button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.brakeButton != null &&
                            this.brakeButton.texture != null &&
                            this.brakeButton.texture.HitTest(t.position)){
                               
                                brakeInput = 1f;
                               
                                Color c = this.brakeButton.texture.color;
                                c.a = this.brakeButton.alphaButtonDown;
                                this.brakeButton.texture.color = c;
                        }
                       
                        // Check if the handbrake button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.handbrakeButton != null &&
                            this.handbrakeButton.texture != null &&
                            this.handbrakeButton.texture.HitTest(t.position)){
                               
                                handbrakeInput = 1f;
                               
                                Color c = this.handbrakeButton.texture.color;
                                c.a = this.handbrakeButton.alphaButtonDown;
                                this.handbrakeButton.texture.color = c;
                        }
                       
                        // Check if the "gear up" button has been pressed this frame
                        if (t.phase == TouchPhase.Began &&
                            this.gearUpButton != null &&
                            this.gearUpButton.texture != null &&
                            this.gearUpButton.texture.HitTest(t.position)){
                               
                                gearUp = true;
                               
                                Color c = this.gearUpButton.texture.color;
                                c.a = this.gearUpButton.alphaButtonDown;
                                this.gearUpButton.texture.color = c;
                        }
                       
                        // Check if the "gear down" button has been pressed this frame
                        if (t.phase == TouchPhase.Began &&
                            this.gearDownButton != null &&
                            this.gearDownButton.texture != null &&
                            this.gearDownButton.texture.HitTest(t.position)){
                               
                                gearDown = true;
                               
                                Color c = this.gearDownButton.texture.color;
                                c.a = this.gearDownButton.alphaButtonDown;
                                this.gearDownButton.texture.color = c;
                        }
                }
               
                // Change gear if necessary
                targetGear = drivetrain.gear;
                if (gearUp && !gearDown){
                        ++targetGear;
                }else if (gearDown && !gearUp){
                        --targetGear;
                }
               
                // Finally, change the button alphas
                if (this.throttleButton != null &&
                        this.throttleButton.texture != null &&
                    throttleInput < Mathf.Epsilon){
                       
                        Color c = this.throttleButton.texture.color;
                        c.a = this.throttleButton.alphaButtonReleased;
                        this.throttleButton.texture.color = c;
                }
               
                if (this.brakeButton != null &&
                        this.brakeButton.texture != null &&
                    brakeInput < Mathf.Epsilon){
                       
                        Color c = this.brakeButton.texture.color;
                        c.a = this.brakeButton.alphaButtonReleased;
                        this.brakeButton.texture.color = c;
                }
               
                if (this.handbrakeButton != null &&
                        this.handbrakeButton.texture != null &&
                    handbrakeInput < Mathf.Epsilon){
                       
                        Color c = this.handbrakeButton.texture.color;
                        c.a = this.handbrakeButton.alphaButtonReleased;
                        this.handbrakeButton.texture.color = c;
                }
               
                if (this.gearUpButton != null &&
                        this.gearUpButton.texture != null &&
                    !gearUp){
                       
                        Color c = this.gearUpButton.texture.color;
                        c.a = this.gearUpButton.alphaButtonReleased;
                        this.gearUpButton.texture.color = c;
                }
               
                if (this.gearDownButton != null &&
                        this.gearDownButton.texture != null &&
                    !gearDown){
                       
                        Color c = this.gearDownButton.texture.color;
                        c.a = this.gearDownButton.alphaButtonReleased;
                        this.gearDownButton.texture.color = c;
                }
        }
}
 


и...
Синтаксис:
Используется csharp
using UnityEngine;

public class AxisCarController : CarController {
               
        public string throttleAxis="Throttle";
        public string brakeAxis="Brake";
        public string steerAxis="Horizontal";
        public string handbrakeAxis="Handbrake";
        public string clutchAxis="Clutch";
        public string shiftUpButton="ShiftUp";
        public string shiftDownButton="ShiftDown";
        public string startEngineButton="StartEngine";
               
               
        protected override void GetInput(out float throttleInput,
                                                                        out float brakeInput,
                                                                        out float steerInput,
                                                                        out float handbrakeInput,
                                                                        out float clutchInput,
                                                                        out bool startEngineInput,
                                                                        out int targetGear){

       
                throttleInput= Input.GetAxisRaw (throttleAxis);
                brakeInput = Input.GetAxisRaw (brakeAxis);
                steerInput = Input.GetAxisRaw (steerAxis);
                handbrakeInput=Input.GetAxisRaw (handbrakeAxis);
                clutchInput =Input.GetAxisRaw (clutchAxis);
                startEngineInput=Input.GetButton (startEngineButton);
               
               
                // Gear shift
                targetGear = drivetrain.gear;
                if(Input.GetButtonDown(shiftUpButton)){
                        ++targetGear;
                }
                if(Input.GetButtonDown(shiftDownButton)){
                        --targetGear;
                }

                if (drivetrain.shifter==true){
                        if(Input.GetButton("reverse")){
                                targetGear=0;
                        }
                       
                        else if(Input.GetButton("neutral")){
                                targetGear=1;
                        }
                       
                        else if(Input.GetButton("first")){
                                targetGear=2;
                        }
                       
                        else if(Input.GetButton("second")){
                                targetGear=3;
                        }
                       
                        else if(Input.GetButton("third")){
                                targetGear=4;
                        }
                       
                        else if(Input.GetButton("fourth")){
                                targetGear=5;
                        }
                       
                        else if(Input.GetButton("fifth")){
                                targetGear=6;
                        }
                       
                        else if(Input.GetButton("sixth")){
                                targetGear=7;
                        }
                       
                        else {
                                targetGear=1;
                        }
                }              
        }
}
 


Я попробовал в MobileCarController добавить кнопки на steering, но чего-то инспектор не реагирует, - ничего не появляется.
Аватара пользователя
Duka
UNец
 
Сообщения: 15
Зарегистрирован: 20 мар 2014, 03:37
Откуда: Харьков, UA
Skype: Deftrax
  • Сайт
  • ICQ

Re: UnityCar MobileCarController без акселерометра

Сообщение ASD 23 апр 2014, 21:16

steerInput = Input.GetAxisRaw (steerAxis); - это закомментировать
и сделать нечто типа
если нажатие кнопки влево
то steerInput=-1
если вправо то steerinput=1
SmartMove Games
ASD
UNIверсал
 
Сообщения: 400
Зарегистрирован: 14 дек 2012, 23:40
Откуда: Харьков
  • ICQ

Re: UnityCar MobileCarController без акселерометра

Сообщение Duka 23 апр 2014, 22:16

Ну вот я сделал, а поля для вставки GUI Texture почему то не появляются в инспекторе.
Аватара пользователя
Duka
UNец
 
Сообщения: 15
Зарегистрирован: 20 мар 2014, 03:37
Откуда: Харьков, UA
Skype: Deftrax
  • Сайт
  • ICQ

Re: UnityCar MobileCarController без акселерометра

Сообщение ASD 24 апр 2014, 08:08

надо добавить 2 объекта GUITexture на сцену, а в скрипт - в самом начале
public GUITexture leftButton;
public GUITexture rightButton;

тогда они появятся в настройках скрипта. Туда перетащить объекты GUITexture. И написать обработчик нажатия на них
SmartMove Games
ASD
UNIверсал
 
Сообщения: 400
Зарегистрирован: 14 дек 2012, 23:40
Откуда: Харьков
  • ICQ

Re: UnityCar MobileCarController без акселерометра

Сообщение Duka 24 апр 2014, 19:43

Успех! Я сделал так:
Синтаксис:
Используется csharp
using UnityEngine;

public class MobileCarController : CarController{
        //-------------------------------------------------------------------------
        // Public class definition
        //-------------------------------------------------------------------------
        [System.Serializable]
        public class TouchButton{
                public GUITexture texture = null;
                public float alphaButtonDown = 0.20f;
                public float alphaButtonReleased = 0.30f;
        }
       
        //-------------------------------------------------------------------------
        // Public class properties
        //-------------------------------------------------------------------------
        public TouchButton throttleButton;
        public TouchButton brakeButton;
        public TouchButton handbrakeButton;
        public TouchButton gearUpButton;
        public TouchButton gearDownButton;
        public TouchButton steerLeftButton;
        public TouchButton steerRightButton;
       
        //-------------------------------------------------------------------------
        // Override Methods
        //-------------------------------------------------------------------------
        protected override void GetInput(out float throttleInput,
                                                                        out float brakeInput,
                                                                        out float steerInput,
                                                                        out float handbrakeInput,
                                                                        out float clutchInput,
                                                                        out bool startEngineInput,
                                                                        out int targetGear){
               
                clutchInput=0;
                startEngineInput=false;
               
                // Read the throttle, brake and handbrake input from touch buttons
                throttleInput = 0f;
                brakeInput = 0f;
                handbrakeInput = 0f;
                bool gearUp = false;
                bool gearDown = false;
                steerInput = 0;        

                // Iterate over all touches...
                int touchCount = Input.touchCount;
                for (int i = 0; i < touchCount; ++i){
                        Touch t = Input.GetTouch(i);
                       
                        // Check if the left button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.steerLeftButton != null &&
                            this.steerLeftButton.texture != null &&
                            this.steerLeftButton.texture.HitTest(t.position)){
                               
                                steerInput = -1;
                               
                                Color c = this.steerLeftButton.texture.color;
                                c.a = this.steerLeftButton.alphaButtonDown;
                                this.steerLeftButton.texture.color = c;
                        }


                        // Check if the right button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.steerRightButton != null &&
                            this.steerRightButton.texture != null &&
                            this.steerRightButton.texture.HitTest(t.position)){
                               
                                steerInput = 1;
                               
                                Color c = this.steerRightButton.texture.color;
                                c.a = this.steerRightButton.alphaButtonDown;
                                this.steerRightButton.texture.color = c;
                        }


                        // Check if the throttle button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.throttleButton != null &&
                            this.throttleButton.texture != null &&
                            this.throttleButton.texture.HitTest(t.position)){
                               
                                throttleInput = 1f;
                               
                                Color c = this.throttleButton.texture.color;
                                c.a = this.throttleButton.alphaButtonDown;
                                this.throttleButton.texture.color = c;
                        }
                       
                        // Check if the brake button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.brakeButton != null &&
                            this.brakeButton.texture != null &&
                            this.brakeButton.texture.HitTest(t.position)){
                               
                                brakeInput = 1f;
                               
                                Color c = this.brakeButton.texture.color;
                                c.a = this.brakeButton.alphaButtonDown;
                                this.brakeButton.texture.color = c;
                        }
                       
                        // Check if the handbrake button is pressed
                        if (t.phase != TouchPhase.Ended &&
                            this.handbrakeButton != null &&
                            this.handbrakeButton.texture != null &&
                            this.handbrakeButton.texture.HitTest(t.position)){
                               
                                handbrakeInput = 1f;
                               
                                Color c = this.handbrakeButton.texture.color;
                                c.a = this.handbrakeButton.alphaButtonDown;
                                this.handbrakeButton.texture.color = c;
                        }
                       
                        // Check if the "gear up" button has been pressed this frame
                        if (t.phase == TouchPhase.Began &&
                            this.gearUpButton != null &&
                            this.gearUpButton.texture != null &&
                            this.gearUpButton.texture.HitTest(t.position)){
                               
                                gearUp = true;
                               
                                Color c = this.gearUpButton.texture.color;
                                c.a = this.gearUpButton.alphaButtonDown;
                                this.gearUpButton.texture.color = c;
                        }
                       
                        // Check if the "gear down" button has been pressed this frame
                        if (t.phase == TouchPhase.Began &&
                            this.gearDownButton != null &&
                            this.gearDownButton.texture != null &&
                            this.gearDownButton.texture.HitTest(t.position)){
                               
                                gearDown = true;
                               
                                Color c = this.gearDownButton.texture.color;
                                c.a = this.gearDownButton.alphaButtonDown;
                                this.gearDownButton.texture.color = c;
                        }
                }
               
                // Change gear if necessary
                targetGear = drivetrain.gear;
                if (gearUp && !gearDown){
                        ++targetGear;
                }else if (gearDown && !gearUp){
                        --targetGear;
                }
               
                // Finally, change the button alphas
                if (this.steerRightButton != null &&
                        this.steerRightButton.texture != null &&
                    steerInput < Mathf.Epsilon){
                       
                        Color c = this.steerRightButton.texture.color;
                        c.a = this.steerRightButton.alphaButtonReleased;
                        this.steerRightButton.texture.color = c;
                }


                if (this.steerLeftButton != null &&
                        this.steerLeftButton.texture != null &&
                    steerInput < Mathf.Epsilon){
                       
                        Color c = this.steerLeftButton.texture.color;
                        c.a = this.steerLeftButton.alphaButtonReleased;
                        this.steerLeftButton.texture.color = c;
                }


                if (this.throttleButton != null &&
                        this.throttleButton.texture != null &&
                    throttleInput < Mathf.Epsilon){
                       
                        Color c = this.throttleButton.texture.color;
                        c.a = this.throttleButton.alphaButtonReleased;
                        this.throttleButton.texture.color = c;
                }
               
                if (this.brakeButton != null &&
                        this.brakeButton.texture != null &&
                    brakeInput < Mathf.Epsilon){
                       
                        Color c = this.brakeButton.texture.color;
                        c.a = this.brakeButton.alphaButtonReleased;
                        this.brakeButton.texture.color = c;
                }
               
                if (this.handbrakeButton != null &&
                        this.handbrakeButton.texture != null &&
                    handbrakeInput < Mathf.Epsilon){
                       
                        Color c = this.handbrakeButton.texture.color;
                        c.a = this.handbrakeButton.alphaButtonReleased;
                        this.handbrakeButton.texture.color = c;
                }
               
                if (this.gearUpButton != null &&
                        this.gearUpButton.texture != null &&
                    !gearUp){
                       
                        Color c = this.gearUpButton.texture.color;
                        c.a = this.gearUpButton.alphaButtonReleased;
                        this.gearUpButton.texture.color = c;
                }
               
                if (this.gearDownButton != null &&
                        this.gearDownButton.texture != null &&
                    !gearDown){
                       
                        Color c = this.gearDownButton.texture.color;
                        c.a = this.gearDownButton.alphaButtonReleased;
                        this.gearDownButton.texture.color = c;
                }
        }
}

И получилось. Раньше была проблема в том что я удалил коды для gearButton, поскольку мне не нужны эти кнопки, но видно что-то осталось, - поэтому часть скрипта с моими кнопками не читалась.
Аватара пользователя
Duka
UNец
 
Сообщения: 15
Зарегистрирован: 20 мар 2014, 03:37
Откуда: Харьков, UA
Skype: Deftrax
  • Сайт
  • ICQ


Вернуться в Скрипты

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 15