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 zico_mahilary · Feb 11, 2014 at 03:53 AM · input.getaxiscar physics

input.getaxis not working

hi, I've got a car script that I've made from this tutorial : link text

but the input.getaxis does not seem to work and the car doesn't move unless i put in values manually into the editor

heres my script :

 //variables
 private var accel : Vector3;
 public var throttle : float;
 private var deadZone = 0.001 ;
 private var myRight : Vector3;
 private var velo : Vector3;
 private var flatVelo : Vector3;
 private var relativeVelocity : Vector3;
 private var dir : Vector3;
 private var flatDir : Vector3;
 private var carUp : Vector3;
 private var carTransform : Transform;
 private var carRigidbody : Rigidbody;
 private var engineForce : Vector3;
 
 private var turnVec : Vector3;
 private var imp : Vector3;
 private var rev : float;
 private var actualTurn : float;
 private var carMass : float;
 private var wheelTransform : Transform[] = new Transform[4];
 public var actualGrip : float;
 public var horizontal : float;
 private var maxSpeedToTurn : float = .2;
 
 //cars wheels
 public var frontLeftWheel : Transform;
 public var frontRightWheel : Transform;
 public var rearLeftWheel : Transform;
 public var rearRightWheel : Transform;
 
 //transforms for steering
 public var LFWheelTransform : Transform;
 public var RFWheelTransform : Transform;
 
 //car physics
 public var power : float = 300;
 public var maxSpeed : float = 50;
 public var carGrip : float = 70;
 public var turnSpeed : float = 3.0;
 
 private var slideSpeed : float;
 public var mySpeed : float;
 
 private var carRight : Vector3;
 private var carFwd : Vector3;
 private var tempVEC : Vector3;
 
 function Start()
     {
     carTransform = transform;
     carRigidbody = rigidbody;
     carUp = carTransform.up;
     carMass = rigidbody.mass;
     carFwd = Vector3.forward;
     carRight = Vector3.right;
     setUpWheels();
     carRigidbody.centerOfMass = Vector3(0,-0.7,0.35);
     }
     
 function Update()
     {
     carPhysicsUpdate();
     checkInput();
     }
 
 function LateUpdate()
     {
     rotateVisualWheels();
     engineSound();
     }
     
 function setUpWheels()
     {
     if((null == frontLeftWheel || null == frontRightWheel || null == rearLeftWheel || null == rearRightWheel))
         {
         Debug.Log("wheel transforms not plugged in");
         //Debug.break;
         }
     else
         {
         wheelTransform[0] = frontLeftWheel;
         wheelTransform[1] = frontRightWheel;            
         wheelTransform[2] = rearLeftWheel;
         wheelTransform[3] = rearRightWheel;
         }
     }
 
 private var rotationAmount : Vector3;
                 
 function rotateVisualWheels()
     {
     LFWheelTransform.localEulerAngles.y = horizontal * 30;
     RFWheelTransform.localEulerAngles.y = horizontal * 30;
     
     rotationAmount = carRight * (relativeVelocity.z*1.6*Time.deltaTime*Mathf.Rad2Deg);
     
     wheelTransform[0].Rotate(rotationAmount);
     wheelTransform[1].Rotate(rotationAmount);
     wheelTransform[2].Rotate(rotationAmount);
     wheelTransform[3].Rotate(rotationAmount);
     }
 
 private var deviceAccelerometerSensitivity : float = 2;
 
 function checkInput()
     {
     if ((Application.platform == RuntimePlatform.IPhonePlayer) || (Application.platform == RuntimePlatform.Android))
         {
         accel = Input.acceleration * deviceAccelerometerSensitivity;
         
         if(accel.x > deadZone || accel.x < -deadZone)
             {
             horizontal = accel.x;
             }
         else
             {
             horizontal = 0;
             }
         throttle = 0;
         
         for (var touch : Touch in Input.touches)
             {
             if(touch.position.x > Screen.width - Screen.width/3 && touch.position.y < Screen.height / 3)
                 {
                 throttle = 1;
                 }
             else if (touch.position.x <Screen.width/3 && touch.position.y < Screen.height / 3)
                 {
                 throttle = -1;
                 }
             }
         }
     
     else if((Application.platform == RuntimePlatform.WindowsEditor) || (Application.platform == RuntimePlatform.WindowsWebPlayer))
         {
         horizontal = Input.GetAxis("Horizontal");
         throttle = Input.GetAxis("Vertical");
         }
     }
 
 function carPhysicsUpdate()
     {
     myRight = carTransform.right;
     velo = carRigidbody.velocity;
     tempVEC = Vector3(velo.x,0,velo.z);
     flatVelo = tempVEC;
     dir = transform.TransformDirection(carFwd);
     tempVEC = Vector3(dir.x,0,dir.z);
     flatDir = Vector3.Normalize(tempVEC);
     relativeVelocity = carTransform.InverseTransformDirection(flatVelo);
     slideSpeed = Vector3.Dot(myRight, flatVelo);
     mySpeed = flatVelo.magnitude;
     rev = Mathf.Sign(Vector3.Dot(flatVelo,flatDir));
     engineForce = (flatDir * (power * throttle) * carMass);
     actualTurn = horizontal;
     
     if (rev<0.1f)
         {
         actualTurn=-actualTurn;
         }
     
     turnVec = (((carUp * turnSpeed) * actualTurn) * carMass) * 800;
     
     actualGrip = Mathf.Lerp(100, carGrip, mySpeed *0.02);
     imp = myRight*(-slideSpeed * carMass * actualGrip);
     }
     
 function slowVelocity()
     {
     carRigidbody.AddForce(-flatVelo * 0.8);
     }
 
 function engineSound()
     {
     audio.pitch = 0.30 + mySpeed * 0.025;
     
     if(mySpeed>30)
         {
         audio.pitch = 0.25 + mySpeed * 0.015;
         }
     if(mySpeed>40)
         {
         audio.pitch = 0.20 + mySpeed * 0.013;
         }
     if(mySpeed>50)
         {
         audio.pitch = 0.15 + mySpeed * 0.011;
         }
     
     if(audio.pitch>2.0)
         {
         audio.pitch = 2.0;
         }
     }
 
 function FixedUpdate()
     {
     if(mySpeed < maxSpeed)
         {
         carRigidbody.AddForce(engineForce*Time.fixedDeltaTime);
         }
     if(mySpeed > maxSpeedToTurn)
         {
         carRigidbody.AddTorque(turnVec*Time.fixedDeltaTime);
         }
     else if(mySpeed < maxSpeedToTurn)
         {
         return;
         }
     carRigidbody.AddForce(imp * Time.fixedDeltaTime);
     }
 

please help...

Comment
Add comment · Show 6
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
avatar image Josh707 · Feb 11, 2014 at 04:11 AM 0
Share

I see 2 potential problems - The 2 windows platforms you are checking for are the web player and windows editor, so it will not be true in a windows build. I'd check for 'RuntimePlatform.WindowsPlayer' as well.

If that's not the problem, check the Input$$anonymous$$anager settings at edit -> project settings -> input and see how the two elements 'Horizontal' and 'Vertical' are set up, this is how they are by default which works with GetAxis (both are same except for the keys you press):

asdfas

avatar image zico_mahilary · Feb 11, 2014 at 04:24 AM 0
Share

hi, i added the 'RuntimePlatform.WindowsPlayer' and checked the input settings too… no effect though… any other suggestions?

 else if((Application.platform == RuntimePlatform.WindowsEditor) || (Application.platform == RuntimePlatform.WindowsWebPlayer) || (Application.platform == RuntimePlatform.WindowsPlayer))
         {
         horizontal = Input.GetAxis("Horizontal");
         throttle = Input.GetAxis("Vertical");
         }
avatar image Josh707 · Feb 11, 2014 at 04:28 AM 0
Share

Hmm, could you post a screenshot of your horizontal/vertical input like I did? Perhaps it's not set up correctly

avatar image zico_mahilary · Feb 11, 2014 at 04:34 AM 0
Share

heres a screenshot of the input settings. alt text

screen shot 2014-02-11 at 10.00.40 am.png (43.1 kB)
avatar image Josh707 · Feb 11, 2014 at 04:52 AM 0
Share

Weird, it's set up correctly. The only other thing I can think of is the if statement isn't having it's requirements met, but it seems like it should work too. Try putting Debug.Log inside the windows platform if statement and see if the code is even being executed.

If it's not, perhaps you might want to check out platform dependent compilation. It's essentially the same as what you're doing but here's a snippet from the docs:

The difference between using RuntimePlatform and Platform dependent Compilation is that using RuntimePlatform is evaluated at runtime while Platform dependent Compilation is evaluated at compile time. If you can use platform dependent compilation, don't hesitate to use it. In most cases, you can get the same functionality using both, and using the defines will produce smaller and faster code, as you don't need to check at runtime

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

facebook with unity problem 0 Answers

My Javascript variables are not visible in the inspector 1 Answer

How to do isometric camera 1 Answer

Hold down 2 buttons and do something 1 Answer

2 errors making a loading scene 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