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 UPSET_ · Mar 22, 2021 at 03:38 PM · playerplayer movementcollisionsterraincollider

Player starts flying when colliding with Terrain

Hello. I am using the Character Controller to Move my player and somehow, if i jump or run against terrain my player gets stuck and is not falling back on the ground again, even if he is not grounded. I have looked this up in the internet for the past days, but haven't found a solution.

Also I am using Mirror, if it has anything to do with that...

Here is a video of my problem: https://www.youtube.com/watch?v=Ve7JbUOO59Q

Looking forward to seeing your creative solutions :)

Movement code:

 using UnityEngine;
 using Mirror;
 using Stranded.Inputs;
 
 public class PlayerMovementController : NetworkBehaviour
 {
     [SerializeField] private CharacterController controller;
     [SerializeField] private float movementSpeed = 5f;
     [SerializeField] private float jumpForce = 2f;
 
     private Vector3 moveDirection = Vector3.zero;
     private Vector2 previousInput;
 
     private Controls controls;
 
     private Animator anim;
 
     public float gravity = 9.81f;
 
     bool jumping = false;
     bool isGrounded = false;
 
     private Controls Controls
     {
         get
         {
             if (controls != null)
             {
                 return controls;
             }
             return controls = new Controls();
         }
     }
 
     public override void OnStartAuthority()
     {
         enabled = true;
         anim = gameObject.GetComponent<Animator>();
 
         //New Input System actions
         Controls.Player.Move.performed += ctx => SetMovement(ctx.ReadValue<Vector2>());
         Controls.Player.Move.canceled += ctx => ResetMovement();
         Controls.Player.Jump.performed += ctx => Jump();
         Controls.Player.Jump.canceled += ctx => JumpCancel();
     }
 
     [ClientCallback]
 
     private void OnEnable()
     {
         Controls.Enable();
     }
     [ClientCallback]
     private void OnDisable()
     {
         Controls.Disable();
     }
     [ClientCallback]
     private void Update()
     {
         Move();
     }
 
     [Client]
     //Vector2 of vertical/horizontal movement gets set
     private void SetMovement(Vector2 movement) => previousInput = movement;
 
     [Client]
 
     private void ResetMovement()
     {
         //reset Vector2 of vertical/horizontal movement
         previousInput = Vector2.zero;
     }
 
     [Client]
     private void Jump()
     {
         jumping = true;
     }
 
     [Client]
     private void JumpCancel()
     {
         jumping = false;
     }
 
     [Client]
 
     private void Move()
     {
         isGrounded = IsGrounded();
         if (isGrounded)
         {
             
             if(previousInput.y == 1)
             {
                 //Forward animation
                 anim.SetInteger("AnimationPar", 1);
             }
             else if(previousInput.y == -1)
             {
                 //Backwards animation
                 anim.SetInteger("AnimationPar", 2);
             }
             else if(previousInput.x == 0 && previousInput.y == 0 && jumping == false)
             {
                 //no inputs, idle animation
                 anim.SetInteger("AnimationPar", 0);
             }
             //Vector3 moveDirection depends on the inputs of previousInput
             moveDirection = new Vector3(previousInput.x, 0, previousInput.y);
             moveDirection = transform.TransformDirection(moveDirection);
 
             //Speed gets applied
             moveDirection *= movementSpeed;
 
             if(jumping == true)
             {
                 //jumping
                 moveDirection.y = jumpForce;
             }
         }
         else
         {
             //Player is not grounded anymore
 
             //Free fall animation
             anim.SetInteger("AnimationPar", 4);
 
             //Gavity gets applied
             moveDirection.y -= gravity * Time.deltaTime;
         }
 
         //Player gets moved by the Character Controller
         controller.Move(moveDirection * Time.deltaTime);
 
     }
 
     [Client]
     //Function to see, if the player is grounded
     private bool IsGrounded()
     {
         float ExtraHeightTest = 0.05f;
         //Raycast a Ray from the player towards the ground
         bool rayCastHit = Physics.Raycast(transform.position, Vector3.down, ExtraHeightTest);
         Color rayColor;
 
         if(rayCastHit == true)
         {
             //Player is grounded
             rayColor = Color.green;
         }
         else
         {
             //Player is not grounded
             rayColor = Color.red;
         }
 
         Debug.DrawRay(transform.position, Vector3.down * (transform.localScale.y / 2 + ExtraHeightTest), rayColor);
         return rayCastHit;
     }
 
 }
 






Comment
Add comment · Show 1
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 $$anonymous$$ · Mar 27, 2021 at 09:14 PM 0
Share

Try using a rigid body player controller

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vertexx · Mar 29, 2021 at 01:17 AM

Hard to tell, but have you adjusted the Slope Limit on your Character controller..if you have one. By your YouTube demo it appears as if the hill slope is too steep for the guy! Try and increase that SlopeLimit? It's default is 45 for moderate slopes but for your steep cliff it should be raised up?

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

113 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

Related Questions

Why does my character vibrates upon starting the game? 0 Answers

Why my character moves left when i press right key ? Controls are upside down? 2 Answers

Moving and rolling a cube 1 Answer

How do I fix collisions while using this PlayerController script? 1 Answer

how to make bots stable? ,how to change bots movement? 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