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 jonaks13 · Feb 17, 2013 at 04:59 AM · playerdirection

Player direction in air

How can I make the player car to not be able to turn and change direction in the air?

Comment
Add comment · Show 9
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 NAKAS · Feb 17, 2013 at 06:21 AM 0
Share

That's a hard question to answer without more information on how your car is made. If it was a rigidbody you can stop that with

 private var grounded = false;
 // This part detects whether or not the object is grounded and stores it in a variable
 function OnCollisionEnter ()
 {
     state ++;
     if(state > 0)
     {
         grounded = true;
     }
 }
  
  
 function OnCollisionExit ()
 {
 state --;
     if(state < 1)
     {
         grounded = false;
         state = 0;
     }
 }
 
 function FixedUpdate ()
 {
 
 if (grounded == false) {
 rigidbody.freezeRotation = true; }}

avatar image jonaks13 · Feb 17, 2013 at 10:51 AM 0
Share

I have some errors with the script after I added it to it.

  var speed : float = 8.0;
     var jumpSpeed : float = 9.0;
     var turnSpeed : float = 90;
     
     private var moveDirection: Vector3 = Vector3.zero;
     private var distGround: float;
     private var isGrounded: boolean = false;
     private var grounded = false;
     
     function Start(){
         rigidbody.freezeRotation = true; // disable physics rotation
         var caps: CapsuleCollider = transform.collider;
         distGround = caps.height / 2 - caps.center.y; // distance to ground
     }
     
     function Update(){
         var hit: RaycastHit; // use raycast to check if character grounded
         isGrounded = Physics.Raycast(transform.position, -transform.up, distGround + 0.1);
         if (isGrounded && Input.GetButtonDown("Jump")){ // only jump from the ground
             rigidbody.velocity.y += jumpSpeed; 
         }
         // rotate character and define direction to move
         transform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);
         moveDirection = -Vector3.forward * Input.GetAxis("Vertical") * speed;
     }
         // This part detects whether or not the object is grounded and stores it in a variable
         function OnCollisionEnter ()
         {
             state ++;
             if(state > 0)
             {
                 grounded = true;
             }
         }
         
         function OnCollisionExit ()
         {
         state --;
             if(state < 1)
             {
                 grounded = false;
                 state = 0;
             }
         }
     
     function FixedUpdate(){ // move at FixedUpdate to get stable results
         transform.Translate(moveDirection * Time.deltaTime);
     }
     {
     
     if (grounded == false); {
     rigidbody.freezeRotation : true; }}

can you help me?

avatar image jonaks13 · Feb 17, 2013 at 12:59 PM 0
Share

I have also another problem that says $$anonymous$$ identifier: 'state' how can I fix it?

avatar image NAKAS · Feb 18, 2013 at 03:57 AM 0
Share

my bad i forgot a state variable.

add private var state = 0; to your code to fix that error.

That whole part of the code was to store grounded to a variable but looking at your script it seems like you already have one. so take out the extra junk that stores the grounded variable and just change the (grounded to (isGrounded on the freeze rotation command.

avatar image jonaks13 · Feb 18, 2013 at 09:48 AM 0
Share

it didn't work. and I have a error when I am changing a little bit in the code that says unexpected identifier 'if'

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by digiben · Feb 17, 2013 at 06:57 AM

You can shoot a ray from the car down and test collision. If the collision happens and the object (the road or whatever) has a distance greater than the root of the car and the bottom of the car, then you know it's above ground and then you set a bool that is used to not allow turning.. basically disable input until the ray that is being cast every frame gives you a road right under the car.

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

12 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

Related Questions

How do I make my player stop moving when I release the button that I am pressing? 0 Answers

Camera rotating super fast when trying to match the camera and players Y euler angle. 1 Answer

How to make a game objective pointer? 1 Answer

Objects Rotation to modify Objects transform velocity/Direction of travel 1 Answer

How to detect two keys released simultaneously [Input System] 1 Answer


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