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 Rovalin · Jan 04, 2013 at 07:08 AM · character controller

Character Controller Slide Action Script!

I'm trying to make a script so that when I push the "S" key my character slides. So that he can slide under objects. But I have no idea where to begin. Is there a way to rotate the character controller capsule so that it is horizontal? That's the only way I can see doing it. Thanks for reading!

-Rov

Comment
Add comment · Show 7
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 AlucardJay · Jan 04, 2013 at 07:08 AM 0
Share

I personally think a slide is a crouch at a run speed with none or very limited left-right movement. From my comment on a similar question : http://answers.unity3d.com/questions/368509/how-to-do-sliding-like-crysis-andor-farcry.html

You somehow have to tap into the character controller and take over, implement something like a crouch with a run speed for a certain period of time, and disable other inputs for that time, then when the time is reached leave the character controller crouched. Here is a great example on how you can tap into the character controller and modify the variables : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

avatar image Rovalin · Jan 05, 2013 at 12:26 AM 0
Share

Thanks allot I'll check those links out!

-Rov

avatar image Rovalin · Jan 05, 2013 at 01:58 AM 0
Share

I'm having a bit of trouble. I have this script here,

function Update(){

var vScale : 1.0;

if (Input.Get$$anonymous$$ey("c")){ vScale = 0.5;

 }

}

But when I apply it to my character nothing happens. What should I do? I don't know exactly what's going on. Thanks you so much though! This is farther then I've gotten before!

-Rov

avatar image Dakwamine · Jan 05, 2013 at 02:36 AM 0
Share

What is vScale? It seems that there is a missing part of your code.

avatar image Rovalin · Jan 05, 2013 at 03:12 AM 0
Share

vScale is what I got from the links posted by alucardj. It's supposed to be the scale of the character controller capsule. So according to the script when the "c" button is pushed the capsule should halve.

-Rov

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by AlucardJay · Jan 05, 2013 at 04:02 AM

Using the crouch and run script by Aldo (awesome script), I have added sliding. Set slideSpeed and slideTimerMax. When sliding, the player can look around, but not change direction.

 #pragma strict
 
 var walkSpeed: float = 7; // regular speed
 var crchSpeed: float = 3; // crouching speed
 var runSpeed: float = 20; // run speed
 
 private var chMotor: CharacterMotor;
 private var ch: CharacterController;
 private var tr: Transform;
 private var height: float; // initial height
 
 public var slideSpeed: float = 20; // slide speed
 private var isSliding : boolean = false;
 private var slideForward : Vector3; // direction of slide
 private var slideTimer : float = 0.0;
 public var slideTimerMax : float = 2.5; // time while sliding
 
 function Start()
 {
     chMotor = GetComponent(CharacterMotor);
     tr = transform;
     ch = GetComponent(CharacterController);
     height = ch.height;
 }
 
 function Update()
 {
     var h = height;
     var speed = walkSpeed;
     
     // - run and crouch -    
     if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")) // press Shift to run
     {
         speed = runSpeed;
     }
     if (Input.GetKey("c")) // press C to crouch
     {
         h = 0.5 * height;
         speed = crchSpeed; // slow down when crouching
     }    
     
     // - slide -    
     if (Input.GetKeyDown("f") && !isSliding) // press F to slide
     {
         slideTimer = 0.0; // start timer
         isSliding = true;
         slideForward = tr.forward;
     }
     if (isSliding)
     {
         h = 0.5 * height; // height is crouch height
         speed = slideSpeed; // speed is slide speed
         chMotor.movement.velocity = slideForward * speed;
         
         slideTimer += Time.deltaTime;
         if (slideTimer > slideTimerMax)
         {
             isSliding = false;
         }
     }    
     
     // - apply movement modifiers -    
     chMotor.movement.maxForwardSpeed = speed; // set max speed
     var lastHeight = ch.height; // crouch/stand up smoothly 
     ch.height = Mathf.Lerp(ch.height, h, 5 * Time.deltaTime);
     tr.position.y += (ch.height - lastHeight) / 2; // fix vertical position
 }


Comment
Add comment · Show 9 · 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 Rovalin · Jan 06, 2013 at 09:04 PM 0
Share

Thanks for all the help! I found another script here that fits into what I'm doing really well. Thanks for the help though!

http://forum.unity3d.com/threads/48187-scale-character-controller-but-not-player

For some reason though, my character likes to fall through the floor.=/

-Rov

avatar image AlucardJay · Jan 07, 2013 at 06:19 AM 0
Share

I have seen other questions with people having trouble with the character collider falling through the terrain, I personally have never seen nor had this problem, and after 10 $$anonymous$$utes I am happy to say this is tested and working. Another question came up, so I did an answer : http://answers.unity3d.com/questions/368509/how-to-do-sliding-like-crysis-andor-farcry.html

Great that you found a way yourself !

avatar image timothy sharp · Jul 27, 2013 at 09:19 PM 0
Share

thx so much

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

This thread is REALLY old, but I still have one question: I don't have a "Character$$anonymous$$oter" on my Player, so how would I lock my Player to only move forward?

avatar image WerewoofPrime · Sep 09, 2021 at 12:37 AM 0
Share

this code is giving me so many compile errors! what language is this in? nothings seems right or where it's supposed to be...

avatar image AlucardJay WerewoofPrime · Sep 09, 2021 at 03:19 PM 0
Share

This is the old uJS aka 'UnityScript', Unity javascript, and worked with the Character Controller component back in Unity 3.5 ~ 4. the way variables are declared and typecast is different, e.g. :

 // uJS
 var variableName : VariableType;
 // C#
 VariableType variableName;

There was also another language before Unity5, called Boo

avatar image WerewoofPrime AlucardJay · Sep 11, 2021 at 12:08 AM 0
Share

oh thank you! so i need to convert this?

Show more comments
avatar image
0

Answer by mardusmordos · Oct 16, 2021 at 09:42 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class slide : MonoBehaviour
 {
    
  
 float walkSpeedt = 7; // regular speed
 float crchSpeed = 3; // crouching speed
 float runSpeed = 20; // run speed
  
  private CharacterMotor chMotor;
  private CharacterController ch;
  private Transform tr;
  private float height; // initial height
  
  public float slideSpeed = 20; // slide speed
  private bool isSliding = false;
  private Vector3 slideForward; // direction of slide
  private float slideTimer = 0.0f;
  public float slideTimerMax = 2.5; // time while sliding
  
  void Start()
  {
      chMotor = GetComponent(CharacterMotor);
      tr = transform;
      ch = GetComponent(CharacterController);
      height = ch.height;
  }
  
  void Update()
  {
     float h = height;
     float speed = walkSpeed;
      
      // - run and crouch -    
      if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")) // press Shift to run
      {
          speed = runSpeed;
      }
      if (Input.GetKey("LeftControl")) // press C to crouch
      {
          h = 0.5f * height;
          speed = crchSpeed; // slow down when crouching
      }    
      
      // - slide -    
      if (Input.GetKeyDown("LeftControl") && !isSliding) //crouch to slide
      {
          slideTimer = 0.0f; // start timer
          isSliding = true;
          slideForward = tr.forward;
      }
      if (isSliding)
      {
          h = 0.5f * height; // height is crouch height
          speed = slideSpeed; // speed is slide speed
          chMotor.movement.velocity = slideForward * speed;
          
          slideTimer += Time.deltaTime;
          if (slideTimer > slideTimerMax)
          {
              isSliding = false;
          }
      }    
      
      // - apply movement modifiers -    
      chMotor.movement.maxForwardSpeed = speed; // set max speed
      float lastHeight = ch.height; // crouch/stand up smoothly 
      ch.height = Mathf.Lerp(ch.height, h, 5 * Time.deltaTime);
      tr.position.y += (ch.height - lastHeight) / 2; // fix vertical position
  }
 }
 

I think it should be it.

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 4 Answers

Help with crouch script! 2 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