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

Sliding in only one direction

Hello, everyone, this 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 this so that you can't move backward when sliding. Here's the movement code I'm using. (This 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.LeftShift))
     {
         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

Hi! Thanks for your answer. I'm not really sure how I would tell what direction I'm moving though...

EDIT: Sorry! I missaw "sideSpeed" as "slideSpeed". I'll try this solution.

avatar image APERSONWHOISAPERSON · Feb 13, 2017 at 03:51 AM 0
Share

This doesn't seem to work... considering I just want the Player to move forward and be able to move in any other direction. (But can still look around)

avatar image jdean300 APERSONWHOISAPERSON · Feb 13, 2017 at 04:01 AM 0
Share

What doesn't work? What happens? This shouldn't affect looking around at all. What was the code you used to implement my solution? I also don't understand "just want the player to move forward and ... in any other direction" - If that's what you're asking about, where does sliding come in?

To be clear, you want the character to only be able to slide in the direction they are moving? Or only towards positive x? What type of game is this? What axes does your character move along?

avatar image APERSONWHOISAPERSON jdean300 · Feb 13, 2017 at 04:38 AM 0
Share

I believe my character moves along the z-axis. And what I mean by "just want the player to move forward and in any other direction" I meant to say only move forward when you are sliding. Nothing "doesn't work", I just want my player to not be able to move backward while sliding. Also, the sliding comes in when isSliding = true. (When I press F, and also the Player crouches when sliding to give it that kinda effect) And the type of game I'm making is a First-Person Platformer game. And yes I want my player to only be able to slide in the direction they're facing.

I'm still pretty new to Unity, so I'm very newb-ish to discussions like these, and I will most likely answer a question incorrectly since I probably won't know what it means.

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.

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

Allow player to slide off of platform, 0 Answers

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

Player still move left&right in MenuUI 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