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 JamessCz · May 18, 2020 at 06:05 AM · movementwallrun

LayerMask wall running? I am trying to create a character controller wallrunning via LayerMask, please help me.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 public CharacterController controller;

 public float speed = 12f;
 public float runSpeed = 24f;
 public float slideSpeedMultiplier = 2;
 public float slideDuration = 1f;
 public float slideDecreaseNumber = 1;
 public float slideSpeedAfterDurationEnd = 8f;
 private float chosenSlideDuration;

 private float slideSpeed;
 private float shiftSlideSpeed;
 private float currentSpeed = 0;

 public Animator slidingAnimator;
 public bool sliding = false;

 [Range(-100,0)]
 public float gravity = -9.81f;
 public float onWallGravity = 0;
 private float normalGravity;
 public float jumpHeight = 3f;

 public Transform groundCheck;
 public float groundDistance = 0.4f;
 public LayerMask groundMask;

 public Transform wallCheckLeft;
 public Transform wallCheckRight;
 public float wallDistance = 0.4f;
 public LayerMask wallMask;

 public Animator speedIndicator;
 public float highSpeed = 45f;

 Vector3 velocity;
 bool isGrounded;
 bool isOnLWall;
 bool isOnRWall;

 void Start()
 {
     slideSpeed = speed * slideSpeedMultiplier;
     shiftSlideSpeed = runSpeed * slideSpeedMultiplier;
     chosenSlideDuration = slideDuration;

     normalGravity = gravity;
 }

 // Update is called once per frame
 void Update()
 {
     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

     if(isGrounded && velocity.y < 0)
     {
         velocity.y = -2f;
     }

     isOnLWall = Physics.CheckSphere(wallCheckLeft.position, wallDistance, wallMask);
     isOnRWall = Physics.CheckSphere(wallCheckRight.position, wallDistance, wallMask);

     if(isOnLWall || isOnRWall)
     {
         //Debug.Log("on the wall");
         gravity = onWallGravity;
     }else{
         gravity = normalGravity;
     }

     float x = Input.GetAxis("Horizontal");
     float z = Input.GetAxis("Vertical");

     Vector3 move = transform.right * x + transform.forward * z;

     SlideCheck();

     if (sliding && Input.GetKey(KeyCode.LeftShift))
     {
         controller.Move(move * shiftSlideSpeed * Time.deltaTime);
         //Debug.Log("sliding with shift (Speed: " + shiftSlideSpeed + ")");
         currentSpeed = shiftSlideSpeed;
         
     }
     else if (sliding)
     {
         controller.Move(move * slideSpeed * Time.deltaTime);
         //Debug.Log("sliding (Speed: " + slideSpeed + ")");
         currentSpeed = slideSpeed;

     }
     else if (Input.GetKey(KeyCode.LeftShift))
     {
         controller.Move(move * runSpeed * Time.deltaTime);
         //Debug.Log("running (Speed: " + runSpeed + ")");
         currentSpeed = runSpeed;
     }
     else
     {
         controller.Move(move * speed * Time.deltaTime);
         //Debug.Log("walking (Speed: " + speed + ")");
         currentSpeed = speed;
     }
     
     if(Input.GetButtonDown("Jump") && isGrounded)
     {
         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
         SlideReset();
     }

     velocity.y += gravity * Time.deltaTime;

     controller.Move(velocity * Time.deltaTime);

     if(currentSpeed >= highSpeed)
     {
         speedIndicator.SetBool("Fast", true);
         Debug.Log("Current speed: " + currentSpeed + " -FAST!");
     }
     else
     {
         speedIndicator.SetBool("Fast", false);
         Debug.Log("Current speed: " + currentSpeed);
     }
 }

 void SlideCheck()
 {

     if (Input.GetKeyDown(KeyCode.LeftControl))
     {
         SlideReset();
     }
     else if (Input.GetKey(KeyCode.LeftControl))
     {
         slideDuration -= Time.deltaTime;
         slideSpeed -= slideDecreaseNumber * Time.deltaTime;
         shiftSlideSpeed -= slideDecreaseNumber * Time.deltaTime;

         sliding = true;
         slidingAnimator.SetBool("Sliding", true);

         if (slideDuration < 0)
         {
             slideSpeed = slideSpeedAfterDurationEnd;
             shiftSlideSpeed = slideSpeedAfterDurationEnd;
         }
     }
     else
     {
         sliding = false;
         slidingAnimator.SetBool("Sliding", false);
     }
 }

 void SlideReset()
 {
     slideSpeed = speed * slideSpeedMultiplier;
     shiftSlideSpeed = runSpeed * slideSpeedMultiplier;
     slideDuration = chosenSlideDuration;
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

263 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 fix my wallrun 0 Answers

How do I create a wallrun where the player is rotated so the wall becomes his new floor? 0 Answers

Why does the FreezeRotation constraint still allow the rotation to be modified? 1 Answer

First person controls for GoogleCardboard 2 Answers

Cant Move towards Right and Jump at same time? 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