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 antonsimmerle · Feb 26 at 07:06 PM · player movementgame development

Defined player hight above the ground

Hey guys, I want that my player always floats about 5 in the Y axis higher than the platform. So also when my player jumps on a higher platform, he automatically sets his position higher than the platform for 5. It's a 3D game. (The player is a ghost).

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by V_0_1_D_ · Feb 26 at 09:09 PM

Unzip and simply attach MovementLogic script to the ghost gameobject. It almost contains all movement aspects. Feel free to modify fields in the inspector. Good Lock :)

alt text

link text

 using UnityEngine;
 
 // A character controller component should be attached to player Gameobject...
 [RequireComponent(typeof(CharacterController))]
 public class MovementLogic : MonoBehaviour
 {
     // Fields to store input from player...
     private float horizontalInput;
     private float verticalInput;
 
     // Vectors to store player Horizontal and Vertical movement...
     private Vector3 horizontalMovement;
     private Vector3 verticalMovement;
 
     // Final movement vector...
     private Vector3 movementDirection;
     private Vector3 movement;
 
     // Player movement speed. Modify in the inspector...
     [Header("Movement Speed")]
     [SerializeField] private float speed = 5.0f;
 
     // How high can player jump? Modify in the inspector...
     [Header("Jump Height")]
     [SerializeField] private float jumpHeight = 0.2f;
 
 
 
     // Gravity Scale. Modify in the inspector...
     [Header("Gravity Scale")]
     [SerializeField] private float gravity = 3.0f;
 
     // How high should the player be of the ground? Modify in the inspector...
     [Header("Character's floating height")]
     [SerializeField] private float floatingHeight = 5.0f;
 
     [Header("Smooth factor")]
     // How smooth should we adjust the character's position above the ground? Modify in the inspector...
     [SerializeField] private float smoothingStep = 0.2f;
 
     // Floating position vector of player. Filled in Awake...
     private Vector3 floatingPosition;
 
     // Control jump status to prevent double jump...
     private bool hasJumped = false;
 
     // Stores jumping vector...
     private Vector3 jumpMovement;
 
     // Has the ray touched ground? This is used instead of isGrounded...
     private bool rayTouchedGround = false;
 
     // Reference to character controller component...
     private CharacterController characterController;
 
     // This method calculates all character movements inside fixed update...
     private void CharacterMovement()
     {
         // Fill in movement vectors with player input...
         horizontalMovement = horizontalInput * transform.right;
         verticalMovement = verticalInput * transform.forward;
 
         // Store movement direction of character...
         movementDirection = horizontalMovement + verticalMovement;
 
         // Clamp movement direction's magnitude to 1 so that diagonal speed does not exceed it...
         movementDirection = Vector3.ClampMagnitude(movementDirection, 1.0f);
 
         // Finally fill in movement vector with direction and speed...
         movement = movementDirection * speed * Time.fixedDeltaTime;
 
         // Move character Horizontally and Vertically...
         characterController.Move(movement);
 
         // Check if player has jumped...
         if (hasJumped)
         {
             // Fill jump movement vector to allow the player go upward...
             jumpMovement.y = jumpHeight;
 
             // Avoid jumping while in air...
             hasJumped = false;
         }
 
         // Apply gravity to player and pull him downwards...
         jumpMovement.y -= gravity * Time.fixedDeltaTime;
 
         // Move character Upwards with jump movement...
         characterController.Move(jumpMovement);
 
         // !!!IMPORTANT: AFTER ALL MOVEMENTS!!!, Make player float above the ground...
         AdjustHeight();
 
         // Not important, just avoid jumpMovement.y from going to low, BUT should be after AdjustHeight call...
         if (rayTouchedGround)
         {
             jumpMovement.y = 0.0f;
         }
     }
 
     private void AdjustHeight()
     {
         // Declare a RaycastHit...
         RaycastHit hit;
 
         // Declare a ray from player's position downwards...
         Ray ray = new Ray(transform.position, Vector3.down);
 
         // Check if the ray has collided with something bellow in amount of 5 or less(i.e. floatHeight)...
         if (Physics.Raycast(ray, out hit, floatingHeight))
         {
             // Smoothly move the character above any platform. Character's position should be in amount of floatingPosition above the ray's hit point...
             transform.position = Vector3.MoveTowards(transform.position, hit.point + floatingPosition, smoothingStep);
 
             // Allow character to jump because he is low enough...
             rayTouchedGround = true;
         }
         else
         {
             // Player is high, dont let him jump...
             rayTouchedGround = false;
         }
     }
 
     private void Awake()
     {
         // Fill in the character controller reference...
         characterController = GetComponent<CharacterController>();
 
         // Initialize floating position vector...
         floatingPosition = new Vector3(0, floatingHeight, 0);
     }
 
     private void FixedUpdate()
     {
         // Call CharacterMovement every fixedUpdate...
         CharacterMovement();
     }
 
     private void Update()
     {
         // Allow character to move and jump only if the player is floatingHeight above the ground. Same as isGrounded...
         if (rayTouchedGround)
         {
             // Get movement input from player...
             horizontalInput = Input.GetAxis("Horizontal");
             verticalInput = Input.GetAxis("Vertical");
 
             // Allow character to jump if the spacebar pressed...
             if (Input.GetButtonDown("Jump"))
             {
                 hasJumped = true;
             }
         }
     }
 }


movementlogic.zip (1.8 kB)
ghost.gif (380.6 kB)
Comment
Add comment · Show 1 · 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 antonsimmerle · Feb 27 at 03:32 PM 1
Share

Thank you so much for your help and patient!! You helped us a lot!! It works perfectly!! Thank you!!

avatar image
0

Answer by antonsimmerle · Feb 26 at 10:44 PM

Thank you for the reply! How can I add raycasts and how do I assign the components to my scene?

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 Caeser_21 · Feb 27 at 04:08 AM 0
Share

I don't think you have to assign anything...
Just add this code to the script and it should work

avatar image antonsimmerle · Feb 27 at 07:33 AM 0
Share

When I only apply the script the ghost doesn't float and falls to ground. What do I have to do?

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

138 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 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

How do I check if a player is holding a trick and not just tapping it (but i want both) 0 Answers

I need to turn my player 90 degrees on mouse click but wanted the turn to be slower while moving along the immediate direction it faces 0 Answers

[C#] When canvas is enabled stop player movement? 0 Answers

Stopping object rotating at a certain rotation 1 Answer

Stopping player move towards button when pressed [2D] 0 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