Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 MattMorrisNC · Feb 05, 2021 at 08:45 PM · movement scriptground detectionspherecollider

How to remove player control while in the air?

I'm working on a ball racing game, and the player is controlled based on force vectors that move the ball in the direction that the player is pushing on either the joystick or arrow keys. Normally, this works fine, but when the ball is knocked into the air, the player can still move the ball from side to side, which isn't what I want. How do I adjust the script below so that the player cannot move from left to right while in the air?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BallControl : MonoBehaviour
 {
     private Rigidbody rb;
     public float accelX = 1.0f;
     public float accelY = 1.0f;
     public float maxSpeed = 50.0f;
     public Transform playerCam;
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         //Get player inputs
         float horizontal = Input.GetAxisRaw("Horizontal");
         float vertical = Input.GetAxisRaw("Vertical");
         
         //Change inputs from world xyz to camera xyz
         Vector3 camF = playerCam.forward;
         Vector3 camR = playerCam.right;
         camF.y = 0.0f;
         camR.y = 0.0f;
         camF = camF.normalized;
         camR = camR.normalized;
         
         //Add force to the ball based on camera position
         Vector3 movement = ((camF * vertical * accelX) + (camR * horizontal * accelY));
         rb.AddForce(movement);
         
         //Speed limit
         if (rb.velocity.magnitude > maxSpeed)
         {
             rb.velocity = rb.velocity.normalized * maxSpeed;
         }
     }
 }
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
1

Answer by matthewroxby · Feb 05, 2021 at 09:49 PM

Just create a bool to see if the ball is touching the ground. Put your movement inputs into an if statement like so:

 if(grounded == false){
       float horizontal = Input.GetAxisRaw("Horizontal")
       float vertical = Input.GetAxisRaw("Vertical")
 }

As for the grounded code, let me know if you don't know how to do that and I can give you some help :)

Comment
Add comment · Show 2 · 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
avatar image MattMorrisNC · Feb 08, 2021 at 04:52 PM 0
Share

Thank you so much! I'm actually a bit new to the C# program$$anonymous$$g language, so I do not know how to create grounded code. If you could give me some instructions that would be awesome!

avatar image matthewroxby MattMorrisNC · Feb 09, 2021 at 08:44 AM 0
Share

So the idea for grounded code is to add a small hitbox underneath your character, and add a grounded script to it. Inside the script, do this:

 void OnTriggerEnter(Collider other){
         //make sure that the collider is a trigger, and set any ground colliders to the ground tag
        if(other.tag == "ground"){
                //get a reference to your player script and set the grounded variable
                player.grounded = true;
 
         }
 
 }

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

115 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision between two sphere game object 1 Answer

AddComponent conforms to shape, size, position of mesh. How do I do this manually? 2 Answers

Colliders not inheriting the Transforms of changed animation states(Run to Crawl) -Unity3D, 0 Answers

how i can Directing transform.rotation to direction of movement character ? 0 Answers

Patrol script won't work. 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