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 j0nk · Apr 17, 2018 at 12:56 PM · raycastsliding

Slope sliding mechanism problem

I'm using an adapted version of the FPSWalkerEnhanced script (http://wiki.unity3d.com/index.php?title=FPSWalkerEnhanced). This is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class PlayerController : MonoBehaviour {
     public CameraController cameraController;
     private CharacterController controller;
     private Transform myTransform;
 
     private float speedX = 0;
     private float speedY = 0;
     private float speedZ = 0;
     private float moveX = 0;
     private float moveZ = 0;
 
     public float acceleration;
     public float decelleration;
     public float walkSpeed;
     public float runSpeed;
     public float gravity;
 
     private float slideLimit;
     private float rayDistance;
     private bool grounded = false;
 
     private Vector3 moveDirection;
     private Vector3 contactPoint;
     private RaycastHit hit;
 
     public float antiBumpFactor = .75f;
 
     void Start () {
         controller = GetComponent<CharacterController>();
         myTransform = transform;
         rayDistance = controller.height * .5f + controller.radius;
         slideLimit = controller.slopeLimit - .1f;
     }
     
     void FixedUpdate () {
         float maxSpeed = Input.GetButton("Run")? runSpeed : walkSpeed;
         float direction = (180 + cameraController.GetViewDirection()) * Mathf.Deg2Rad;
         float forward = (Input.GetKey("w") ? 1 : 0) + (Input.GetKey("s") ? -1 : 0);
         float right = (Input.GetKey("d") ? 1 : 0) + (Input.GetKey("a") ? -1 : 0);
         moveX = Mathf.Sin(direction) * right + Mathf.Cos(direction) * forward;
         moveZ = Mathf.Sin(direction) * forward - Mathf.Cos(direction) * right;
 
         float ratio = Mathf.Sqrt (Mathf.Pow(moveX, 2) + Mathf.Pow(moveZ, 2))/maxSpeed;
         if (ratio != 0) {
             moveX = moveX / ratio;
             moveZ = moveZ / ratio;
         }
 
         if (moveX > speedX)
             speedX = Mathf.Min (speedX + acceleration*Time.deltaTime, moveX);
         else if (moveX < speedX)
             speedX = Mathf.Max (speedX - decelleration*Time.deltaTime, moveX);
 
         if (moveZ > speedZ)
             speedZ = Mathf.Min (speedZ + acceleration*Time.deltaTime, moveZ);
         else if (moveZ < speedZ)
             speedZ = Mathf.Max (speedZ - decelleration*Time.deltaTime, moveZ);
 
         moveDirection = new Vector3 (speedX, 0, speedZ);
         
         if (grounded) {
                         //sliding stuff
             bool sliding = false;
             if (Physics.Raycast(myTransform.position, -Vector3.up, out hit, rayDistance)) {
                 if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
                     sliding = true;
             } else {
                 Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, out hit);
                 if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
                     sliding = true;
             }
 
             if (sliding) {
                 print ("true");
                 Vector3 hitNormal = hit.normal;
                 moveDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z);
                 Vector3.OrthoNormalize (ref hitNormal, ref moveDirection);
                 moveDirection *= 5;
             } else {
                 print ("false");
                 moveDirection.y = -antiBumpFactor;
             }
         }
         moveDirection.y -= gravity;
 
         grounded = (controller.Move(moveDirection*Time.deltaTime) & CollisionFlags.Below) != 0;
     }
 
     void OnControllerColliderHit (ControllerColliderHit hit) {
         contactPoint = hit.point;
     }
 }

However, the sliding mechanism glitches out edges of cubes and planes, making it impossible to jump down from them. alt text I am new to unity, so I'm finding it hard to solve this problem. Does anyone know a solution?

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
0
Best Answer

Answer by j0nk · Apr 17, 2018 at 01:46 PM

After some debugging I found out that the RayCast was colliding with the character's own controller. This was solved by adding a condition:

 if (Physics.Raycast(myTransform.position, -Vector3.up, out hit, rayDistance)) {
                 if (Vector3.Angle (hit.normal, Vector3.up) > slideLimit && hit.collider != controller) 
                     sliding = true;
             } else {
                 Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, out hit);
                 if (Vector3.Angle (hit.normal, Vector3.up) > slideLimit && hit.collider != controller)
                     sliding = true;
             }
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

108 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

Related Questions

How to add a value to a Raycast Vector 1 Answer

Gravity makes player slide on any slope!!! Check if rigidbody Isgrounded not working 0 Answers

Sliding Problem Tomb Raider like game C# 1 Answer

How to make third person character aim at object in front of camera? 1 Answer

Spawning object by using ray 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