Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by l4rthurl · Apr 15, 2014 at 07:15 AM · inputgui-button

how to use gui buttons as input get axis for mobile?

I tried some ways to do it, but they didn't worked. Here is my scripts (based on the tutorial series car controller kit, by flat tutorial), I started converting them to use in mobile, and I already got to set up the acelerometer, but the touch inputs are still missing.

WheelScript:

 enum wheelType { Steer , SteerAndMotor , Motor , JustAWheel}; 
 var typeOfWheel : wheelType;    
 var handBreakable : boolean = false;    
 var invertSteer : boolean = false;    
 var wheelTransform : Transform;        
 var zeroAc: Vector3;
 var curAc: Vector3;
 var sensH: float = 10;
 var sensV: float = 10;
 var smooth: float = 0.5;
 var GetAxisH: float = 0;
 var GetAxisV: float = 0;
 private var speedFactor : float;    
 private var wheelCollider : WheelCollider;        
 private var carScript : CarControleScript;        
 private var mySidewayFriction : float;    
 private var myForwardFriction : float;    
 private var slipSidewayFriction : float;    
 private var slipForwardFriction : float;    
 private var axisH: float = 0;
 private var axisV: float = 0;
 
 function ResetAxes(){
     zeroAc = Input.acceleration;
     curAc = Vector3.zero;
     }
 
 function Start () {
 wheelCollider = gameObject.GetComponent(WheelCollider);
 carScript = transform.root.gameObject.GetComponent("CarControleScript");
 SetValues();
 ResetAxes();
 }
 
 
 function SetValues(){
 
 myForwardFriction  = wheelCollider.forwardFriction.stiffness;
 mySidewayFriction  = wheelCollider.sidewaysFriction.stiffness;
 slipForwardFriction = 0.05;
 slipSidewayFriction = 0.085;
 
 }
 
 
 function Update () {
 WheelPosition();
 ReverseSlip();
 
 wheelTransform.Rotate(wheelCollider.rpm/60*360*Time.deltaTime,0,0);
 if (typeOfWheel == wheelType.Steer || typeOfWheel == wheelType.SteerAndMotor)
 wheelTransform.localEulerAngles.y = wheelCollider.steerAngle - wheelTransform.localEulerAngles.z;
     
 curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
     GetAxisV = Mathf.Clamp(curAc.y * sensV, -1, 1);
     GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 1);
 }
 
 
 
 function FixedUpdate (){
 if (typeOfWheel == wheelType.Motor || typeOfWheel == wheelType.SteerAndMotor){
 TorqueControle ();
 }
 if (typeOfWheel == wheelType.Steer || typeOfWheel == wheelType.SteerAndMotor){
 SteerControle ();
 }
 if (handBreakable){
 HandBrake();
 }
 if(!carScript.braked){
 Decellaration();
 }
 }
 
 function InputGetAxis(axis: String): float {
      var v = Input.GetAxis(axis);
     if (Mathf.Abs(v) > 0.005) return v;
     if (axis=="Horizontal") return axisH;
     if (axis=="Vertical") return axisV;
 }
 function OnGUI() {
 
     axisV = axisH = 0;
     if (GUI.RepeatButton( Rect( 10, 435, 100, 35 ), "Forward" ) ) axisV = 1;
     if (GUI.RepeatButton( Rect( 690, 435, 100, 35 ), "Backward" ) ) axisV = -1;
 }
 
 
 function WheelPosition(){
 var hit : RaycastHit;
 var wheelPos : Vector3;
 
 if (Physics.Raycast(transform.position, -transform.up,hit,wheelCollider.radius+wheelCollider.suspensionDistance) ){
 wheelPos = hit.point+transform.up * wheelCollider.radius;
 }
 else {
 wheelPos = transform.position -transform.up* wheelCollider.suspensionDistance; 
 }
 wheelTransform.position = wheelPos;
 }
 
 
 
 
 function Decellaration(){
 if (Input.GetButton("Vertical")==false){
 wheelCollider.brakeTorque = carScript.decellarationSpeed;
 }
 else{
 wheelCollider.brakeTorque = 0;
 }
 }
 
 
 
 function SteerControle (){
 speedFactor = transform.parent.root.rigidbody.velocity.magnitude/carScript.lowestSteerAtSpeed;
 var currentSteerAngel = Mathf.Lerp(carScript.lowSpeedSteerAngel,carScript.highSpeedSteerAngel,speedFactor);
 if (invertSteer)
 currentSteerAngel *= -GetAxisH;
 else 
 currentSteerAngel *= GetAxisH;
 wheelCollider.steerAngle = currentSteerAngel;
 }
 
 
 
 
 function TorqueControle (){
 if (carScript.currentSpeed < carScript.topSpeed && carScript.currentSpeed > -carScript.maxReverseSpeed && !carScript.braked){
 wheelCollider.motorTorque = carScript.maxTorque * Input.GetAxis( "Vertical" );
 }
 else {
 wheelCollider.motorTorque =0;
 }
 }
 
 
 function HandBrake(){
 if (carScript.braked){
 if (carScript.currentSpeed > 1){
 SetRearSlip(slipForwardFriction ,slipSidewayFriction); 
 }
 else if (carScript.currentSpeed < 0){
 SetRearSlip(1 ,1); 
 }
 wheelCollider.brakeTorque = carScript.maxBrakeTorque;
 wheelCollider.motorTorque =0;
 if (carScript.currentSpeed < 1 && carScript.currentSpeed > -1){
 carScript.backLightObject.renderer.material = carScript.idleLightMaterial;
 }
 else {
 carScript.backLightObject.renderer.material = carScript.brakeLightMaterial;
 }
 }
 else {
 wheelCollider.brakeTorque = 0;
 SetRearSlip(myForwardFriction ,mySidewayFriction); 
 }
 }
 
 
 function ReverseSlip(){
 if (carScript.currentSpeed <0){
 SetFrontSlip(slipForwardFriction ,slipSidewayFriction); 
 }
 else {
 SetFrontSlip(myForwardFriction ,mySidewayFriction);
 }
 }
 
 
 function SetRearSlip (currentForwardFriction : float,currentSidewayFriction : float){
 if (typeOfWheel == wheelType.Motor || typeOfWheel == wheelType.SteerAndMotor && !carScript.braked){
 wheelCollider.forwardFriction.stiffness = currentForwardFriction;
 wheelCollider.sidewaysFriction.stiffness = currentSidewayFriction;
 }
 }
 function SetFrontSlip (currentForwardFriction : float,currentSidewayFriction : float){
 if (typeOfWheel == wheelType.Steer || typeOfWheel == wheelType.SteerAndMotor && !carScript.braked){
 wheelCollider.forwardFriction.stiffness = currentForwardFriction;
 wheelCollider.sidewaysFriction.stiffness = currentSidewayFriction;
 }
 }

Car controle script:

 var centerOfMass : Vector3;    //Center of mass
     var dataWheel : WheelCollider;    //Wheel Collider from which you want to calculate the speed
     var lowestSteerAtSpeed : float = 50;    //if lowestSteerAtSpeed < currentSpeed the steer Angle = highSpeedSteerAngel
     var lowSpeedSteerAngel : float = 10;    //This could be a high value
     var highSpeedSteerAngel : float = 1;    //This shouldn't be a high value (recomended for stability of car)
     var decellarationSpeed : float = 30;    //How fast the car will decellarate
     var maxTorque : float  = 50;    //Maximum Torque
     var currentSpeed : float;        //Current Speed of car
     var topSpeed : float = 150;        //Highest speed at which the car can go
     var maxReverseSpeed : float = 50;     //Highest Reverse speed
     var backLightObject : GameObject;    //Mesh for reverse light
     var idleLightMaterial : Material;    //for idle state
     var brakeLightMaterial : Material;     //Braked state
     var reverseLightMaterial : Material;    //Reverse state
     @HideInInspector    
     var braked : boolean = false;    //Brake trigger
     var maxBrakeTorque : float = 100;     //Braking speed
     var speedOMeterDial : Texture2D;    //GUI Texture for dial
     var speedOMeterPointer : Texture2D;        //GUI Texture for needle
     var gearRatio : int[];        //Shift gear at speed
     var spark : GameObject;        //OnCollision Spark
     var collisionSound : GameObject;    //OnCollision Sound
     private var axisH: float = 0;
     private var axisV: float = 0;
     
     function Start () {
     rigidbody.centerOfMass=centerOfMass; //Center of mass , for this the car should be pointing on z axis
     }
     
     function FixedUpdate () {
     HandBrake();
     }
     function Update(){
     BackLight ();
     EngineSound();
     CalculateSpeed();
     
          
     }
     
     
     function CalculateSpeed(){
     currentSpeed = 2*22/7*dataWheel.radius*dataWheel.rpm*60/1000;
     currentSpeed = Mathf.Round(currentSpeed);
     }
     
     
     function BackLight (){
     if (currentSpeed > 0 && Input.GetAxis("Vertical")<0&&!braked){
     backLightObject.renderer.material = brakeLightMaterial;
     }
     else if (currentSpeed < 0 && Input.GetAxis("Vertical")>0&&!braked){
     backLightObject.renderer.material = brakeLightMaterial;
     }
     else if (currentSpeed < 0 && Input.GetAxis("Vertical")<0&&!braked){
     backLightObject.renderer.material = reverseLightMaterial;
     }
     else if (!braked){
     backLightObject.renderer.material = idleLightMaterial;
     }
     }
     
     
     function HandBrake(){
     if (Input.GetButton("Jump")){
     braked = true;
     }
     else{
     braked = false;
     }
     }
     
     
     function EngineSound(){
     for (var i = 0; i < gearRatio.length; i++){
     if(gearRatio[i]> currentSpeed){
     break;
     }
     }
     var gearMinValue : float = 0.00;
     var gearMaxValue : float = 0.00;
     if (i == 0){
     gearMinValue = 0;
     }
     else {
     gearMinValue = gearRatio[i-1];
     }
     gearMaxValue = gearRatio[i];
     var enginePitch : float = ((currentSpeed - gearMinValue)/(gearMaxValue - gearMinValue))+1;
     audio.pitch = enginePitch;
     }
     
     function InputGetAxis(axis: String): float {
      
         var v = Input.GetAxis(axis);
         if (Mathf.Abs(v) > 0.005) return v;
         if (axis=="Horizontal") return axisH;
         if (axis=="Vertical") return axisV;
        } 
     
     
     function OnGUI (){
     GUI.DrawTexture(Rect(Screen.width - 300,Screen.height - 150,300,150),speedOMeterDial);
     var speedFactor : float = currentSpeed / topSpeed;
     var rotationAngle : float;
     if (currentSpeed >= 0){
       rotationAngle = Mathf.Lerp(0,180,speedFactor);
       }
       else {
       rotationAngle = Mathf.Lerp(0,180,-speedFactor);
       }
     GUIUtility.RotateAroundPivot(rotationAngle,Vector2(Screen.width - 150 ,Screen.height));
     GUI.DrawTexture(Rect(Screen.width - 300,Screen.height - 150,300,300),speedOMeterPointer);
     
     axisV = axisH = 0;
         if (GUI.RepeatButton( Rect( 10, 435, 100, 35 ), "Forward" ) ) axisV = 1;
         if (GUI.RepeatButton( Rect( 690, 435, 100, 35 ), "Backward" ) ) axisV = -1;
     }
     
     
     function OnCollisionEnter (other : Collision){
     
     if (other.transform != transform && other.contacts.length != 0){
     for (var i = 0; i < other.contacts.length ; i++){
     Instantiate(spark,other.contacts[i].point,Quaternion.identity);
     var clone : GameObject = Instantiate(collisionSound,other.contacts[i].point,Quaternion.identity);
     clone.transform.parent = transform;
     }
     }
     }
     
     function OnDrawGizmos  () {
             Gizmos.color = Color.white;
             Gizmos.DrawWireSphere (transform.position+centerOfMass, 0.1);
         }

Does anyone knows how to use the gui buttons as input get axis, or how to convert this script to use natively acelerometer for steering and buttons for acelerate, brake/reverse, and hand brake?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BlackPanda · Apr 15, 2014 at 08:33 AM

If you are using a GUITexture, then use the method HitTest()

https://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=hittest

Using this method, you can check whether a particular point is inside a GUI element.

N.B: There are already some posts about this in Answers. You didn't use the search functionality effectively.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

List the Index of an Array as Buttons 1 Answer

How could I implement cheat codes in my game? 2 Answers

How do I invert the Y axis in Penelope tutorial? 3 Answers

Input Shift name 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges