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
1
Question by JuanseCoello · Mar 13, 2014 at 09:42 PM · movementraycastcharactersliding

Sliding Problem Tomb Raider like game C#

I am making a tomb raider like game. Where the character slides in slopes that are too inclinated. I have a script and have followed the instructions of 3d buzz. But the sliding part doesnt work, I have noticed that if I remove some parts of the code it works, but it slides indefinetly, so maybe what I need is a Raycast that stops the player from sliding when it touches the horizontal plane. Here is the code. I have two scripts that work toghether when attached a character. The TP_Controller2 script wich is not very important, but if you want to test how a character can move go on and attach it to a capsule or character. Somehow this script no longer works in the new unity, but only the sliding part, everything else works fine.

  using UnityEngine;
  using System.Collections;

  public class TP_Controller2 : MonoBehaviour 
  {
 public static CharacterController CharacterController;
 public static TP_Controller2 Instance;
 
 void Awake() 
 {
     CharacterController = GetComponent("CharacterController") as         CharacterController;
     Instance = this;
 }
 
 void Update() 
 {
     if(Camera.mainCamera == null)
         return;
     
     GetLocomotionInput();
     HandleActionInput();
     
     TP_Motor2.Instance.UpdateMotor();
 }
 
 void GetLocomotionInput()
 {
     var deadZone = 0.1f;

     TP_Motor2.Instance.VerticalVelocity = TP_Motor2.Instance.MoveVector.y;
     TP_Motor2.Instance.MoveVector = Vector3.zero;
     
     if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
         TP_Motor2.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
     
     if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
         TP_Motor2.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
 }

 void HandleActionInput()
 {

     if (Input.GetKeyDown(KeyCode.Space))
     {
         Jump();
     }

 }

 public void Jump()
 {
     if (!CharacterController.isGrounded)
         return;

     TP_Motor2.Instance.Jump();

 }

}

Then there is the TP_Motor2 script, this is important because here it is where the sliding part is. Under void ApplySlide() If you take away this line: slideDirection = Vector3.zero; the character slides, but it slides without stoping. So maybe the solution is that something must be added to recognize the normal of the floor and stop the character from moving. Here is the script.

     using UnityEngine;
     using System.Collections;

public class TP_Motor2 : MonoBehaviour { public static TP_Motor2 Instance;

 public float MoveSpeed = 10f;
 public float JumpSpeed = 16f;
 public float Gravity = 31f;
 public float TerminalVelocity = 40f;
 public float SlideThreshold = 0.6f;
 public float MaxControllableSlide = 0.4f;

 private Vector3 slideDirection;
 
 public Vector3 MoveVector { get; set; }
 public float VerticalVelocity { get; set; }
 
 void Awake() 
 {
     Instance = this;
 }
 
 public void UpdateMotor() 
 {
     ProcessMotion();
 }
 
 void ProcessMotion()
 {
     // Transform MoveVector to World Space
     MoveVector = transform.TransformDirection(MoveVector);
     
     // Normalize MoveVector if Magnitude > 1
     if (MoveVector.magnitude > 1)
         MoveVector = Vector3.Normalize(MoveVector);

     // Applyslide if applicable
     ApplySlide();

     // Multiply MoveVector by MoveSpeed
     MoveVector *= MoveSpeed;

     // Reapply VerticalVelocity MoveVector.y
     MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);

     // Apply Gravity
     ApplyGravity();

     // Move Character in World Space
     TP_Controller2.CharacterController.Move(MoveVector * Time.deltaTime);
 }

 void ApplyGravity()
 {
     if (MoveVector.y > -TerminalVelocity)
         MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
     if (TP_Controller2.CharacterController.isGrounded && MoveVector.y < -1)
         MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
 }


 void ApplySlide() // THIS IS THE PART THAT MUST BE CHANGED
 {
     if (!TP_Controller2.CharacterController.isGrounded)
         return;

     slideDirection = Vector3.zero;

     RaycastHit hitInfo;
     if (Physics.Raycast(transform.position + Vector3.up, Vector3.down, out hitInfo))
     {
         if (hitInfo.normal.y < SlideThreshold)
             slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
     }

     if (slideDirection.magnitude < MaxControllableSlide)
         MoveVector += slideDirection;
     else
     {
         MoveVector = slideDirection;
     }
 }

 public void Jump()
 {
     if (TP_Controller2.CharacterController.isGrounded)
         VerticalVelocity = JumpSpeed;
 }

}

Comment
Add comment · Show 1
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 $$anonymous$$ · Feb 08, 2017 at 05:38 PM 0
Share

@JuanseCoello Any progress? Some problem here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Feb 08, 2017 at 05:38 PM

@JuanseCoello any progress? Some problem here.

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

20 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

Related Questions

Changing the landing and switch directions of standard assets TPC controller 0 Answers

movement of the charater 1 Answer

Character Rotation 2 Answers

Velocity for movement 0 Answers

Why does my character keep sliding around? Help! (Rigid Body) 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