- Home /
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).
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 :)
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;
}
}
}
}
Thank you so much for your help and patient!! You helped us a lot!! It works perfectly!! Thank you!!
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?
I don't think you have to assign anything...
Just add this code to the script and it should work
When I only apply the script the ghost doesn't float and falls to ground. What do I have to do?