• 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 /
  • Help Room /
avatar image
0
Question by APERSONWHOISAPERSON · Feb 13, 2017 at 02:16 AM · charactercontrollerdirectionslidingone

Sliding in only one direction

Hello, everyone, t$$anonymous$$s is my second time asking a question here. So what I'm trying to do is when the character slides, he only slides in one direction. I want to do t$$anonymous$$s so that you can't move backward when sliding. Here's the movement code I'm using. (T$$anonymous$$s is a first person game using the Character Controller for moving if that's important)

  1. using UnityEngine; using System.Collections;

public class Movement : MonoBehaviour { public float moveSpeed; public float runSpeed; public float jumpPower; float gravity = -9.81f; float verticalVel = 0;

 CharacterController controller;

 float horizontalRotation;
 float verticalRotation;
 public float upDownRange;
 public float mouseSensX;
 public float mouseSensY;

 float distToCeiling;

 private bool crouchCollision = false;
 public bool isRunning;

 public bool isSliding;
 public float slideSpeed = 10.0f;
 private float slideTimer = 0.0f;
 public float slideTimerMax = 1.5f;

 void Start()
 {
     controller = GetComponent<CharacterController> ();
     Cursor.visible = false;
     Cursor.lockState = CursorLockMode.Locked;
     distToCeiling = GetComponent<CapsuleCollider> ().bounds.extents.y;
 }

 void Update()
 {
     //Rotation
     horizontalRotation = Input.GetAxis ("Mouse X") * mouseSensX;
     transform.Rotate (0, horizontalRotation, 0);

     verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensY;
     verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
     Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);

     //Movement
     float forwardSpeed = Input.GetAxis ("Vertical") * moveSpeed;
     float sideSpeed = Input.GetAxis ("Horizontal") * moveSpeed;

     if (controller.isGrounded) 
     {
         verticalVel = 0;
     }

     if (Input.GetKeyDown (KeyCode.Space) && controller.isGrounded)
     {
         verticalVel = jumpPower;
     }

     verticalVel += gravity * Time.deltaTime;

     Vector3 speed = new Vector3 (sideSpeed, verticalVel, forwardSpeed);

     speed = transform.rotation * speed;

     controller.Move (speed * Time.deltaTime);

     //Other Stuff
     if (Input.GetKey (KeyCode.LeftS$$anonymous$$ft))
     {
         moveSpeed = runSpeed;
         isRunning = true;
     }
     else
     {
         moveSpeed = 5f;
         isRunning = false;
     }

     if (Physics.Raycast (transform.position, transform.up, 1.5f))
     {
         crouchCollision = true;
     } 
     else
     {
         crouchCollision = false;
     }

     if (!isSliding && crouchCollision) {
         Crouch ();
     } else if (isSliding && crouchCollision) {
         Crouch ();
     } else {
         UnCrouch ();
     }

     if (Input.GetKeyDown (KeyCode.F) && !isSliding && isRunning) 
     {
         slideTimer = 0.0f;
         isSliding = true;
     } 

     if (isSliding) {
         Slide ();
     }   
 }

 void Slide()
 {
     Crouch ();
     moveSpeed = slideSpeed;

     slideTimer += Time.deltaTime;
     if (slideTimer > slideTimerMax) {
         isSliding = false;
         slideTimer = 0.0f;
     }
 }

 void Crouch()
 {
     controller.height = 1.0f;
     jumpPower = 0f;
 }

 void UnCrouch()
 {
     controller.height = 2.0f;
     jumpPower = 6f;
 }

 void FixedUpdate()
 {
     Vector3 up = transform.TransformDirection (Vector3.up);

     if (Physics.Raycast (transform.position, up, distToCeiling + 0.1f))
     {
         verticalVel = 0;
     }
 }

}

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

Answer by jdean300 · Feb 13, 2017 at 02:28 AM

Just check your sideSpeed variable before setting isSliding to true - if the sideSpeed is going the wrong direction, do not make isSliding true. As far as determining what the wrong direction is, you can check the sign of sideSpeed compared to controller.velocity.x. If they have different signs, then don't let them slide.

Comment
Add comment · Show 7 · 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 APERSONWHOISAPERSON · Feb 13, 2017 at 02:39 AM 0
Share
avatar image APERSONWHOISAPERSON · Feb 13, 2017 at 03:51 AM 0
Share
avatar image jdean300 APERSONWHOISAPERSON · Feb 13, 2017 at 04:01 AM 0
Share
avatar image APERSONWHOISAPERSON jdean300 · Feb 13, 2017 at 04:38 AM 0
Share
Show more comments
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 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

Related Questions

Unity CharacterController.Move and Slopes. Sliding down steep slopes and stop bouncing down non steep slopes. 1 Answer

Controller Rotation returns to Zero when there is no movement. 0 Answers

Using "AddForce" with a Character Controller 1 Answer

Allow player to slide off of platform, 0 Answers

Movement bugs after reopening unity project 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