Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 krythix · Mar 11, 2020 at 10:54 AM · floatinghovering

Why is my cube 'floating' above the ground?

Here is my code (I am following along with a video tutorial series)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     //This script will control our player movement in our game
 
     public float moveSpeed;
     public float jumpForce;
 
     private Rigidbody rig;
 
     private void Awake()
     {
         //get the rigidbody component
         rig = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         Move();
 
         if(Input.GetButtonDown("Jump"))
         {
             TryJump();
         }
     }
 
     void Move()
     {
         //getting our player control inputs
         float xInput = Input.GetAxis("Horizontal"); //will return an X value of -1 to 1. If no button is pressed then value will be 0
         float zInput = Input.GetAxis("Vertical");//will return a Y value of -1 to 1. If no button is pressed then value will be 0
 
         Vector3 dir = new Vector3(xInput, 0, zInput) * moveSpeed;
         dir.y = rig.velocity.y; //setting y to whatever existing velocity is
 
         rig.velocity = dir; //applying our Vector3 to our rigidbody
         //note: the 'velocity' is a Rigid3 itself as well ^
 
         Vector3 facingDir = new Vector3(xInput, 0, zInput); // 'magnitude' is this vector's length
 
         if(facingDir.magnitude > 0) //if magnitude is NOT zero, then we change the facing direction forward with this code
         {
             transform.forward = facingDir;
         }
     }
 
     void TryJump()
     {
 
         Ray ray = new Ray(transform.position, Vector3.down); //we are shooting the raycast in a downward position to check if we are on the floor
 
         if(Physics.Raycast(ray, 0.7f)) // first value 'ray' is the direction we're shooting in, 0.7f is how faf we are shooting the raycast
         {
             rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
         }
     }
 }


This is for just a default Unity 3D object > create new > 'Cube'.

Position: 0, 0, 0 Rotation: 0, 0 ,0 Scale: 1, 1, 1

For some reason my cube object is 'hovering' or floating just ever so slightly above the ground. The bottom of the cube is not touching the ground object.

My cube object (which serves as the main controllable player) has Rigidbody and Box Collider.

I am very new so sorry if this seems simple.

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

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

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

Related Questions

Maintain height/Altitude/distance from ground? 2 Answers

Hovering smoothly at one height 1 Answer

Simulating Water not working.. 1 Answer

3rd person 1 Answer

Making an object float in water 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