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 JuanseCoello · Apr 01, 2014 at 08:44 PM · rigidbodyraycastslidingisgrounded

Gravity makes player slide on any slope!!! Check if rigidbody Isgrounded not working

I have two scripts, they make the character move horizontally and vertically, and I have added gravity but the character keeps sliding on solpes, I want to stop that so the character never slides. By the way the void is grounded doesnt help very well. Also can you help me understand how to make the void IsGrounded work, so unity detects if the rigidbosy is Grounded? You can test this script in a player. Remember that you have to add a rigidbody, and a collider, and also disable rigidbody's rotations.

Here are my two scripts:

1st script TP_Motor

    using UnityEngine;
    using System.Collections;

public class TP_Motor : MonoBehaviour {

 public static TP_Motor Instance;

 public float MoveSpeed = 40f;

 public float Gravity = 10f;

 public float TerminalVelocity = 20f;

 public Vector3 MoveVector { get; set; }
 public float VerticalVelocity { get; set; }

 void Awake()  // Debe quedarse en awake
 {
     Instance = this;
 }
 

 public void UpdateMotor()  // Debe quedarse en update motor
 {
     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);

     // 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_Controller.Rigidbody2.velocity = MoveVector;

 }

 void ApplyGravity()
 {

     if (MoveVector.y > -TerminalVelocity)
         MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
     if ((TP_Controller.Instance.isgrounded == false) && MoveVector.y < -1)
         MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
 }

}

2nd script TP_Controller

using UnityEngine; using System.Collections;

public class TP_Controller : MonoBehaviour { public static Rigidbody Rigidbody2; public static TP_Controller Instance;

 public bool isgrounded;

 private float distToGround;

 void Awake () // Debe quedarse en awake
 {

     Rigidbody2 = GetComponent("Rigidbody") as Rigidbody;
     Instance = this;
     distToGround = collider.bounds.extents.y;
 }
 

 void Update () 
 {
     if (Camera.mainCamera == null)
         return;

     GetLocomotionInput();

     TP_Motor.Instance.UpdateMotor();
 }

 void IsGrounded()
 {

     if (Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f))
         isgrounded = true; // Debe mantenerse en true
 }

 void GetLocomotionInput()
 {
     var deadZone = 0.1f;

     TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
     TP_Motor.Instance.MoveVector = Vector3.zero;

     if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
         TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));

     if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
         TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 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

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

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

How to check if rigidbody is grounded with raycasts on uneven surfaces? 2 Answers

Attaching 2 objects using joints and raycasting 0 Answers

raycast rigidbody addforce 2 Answers

How to get right Physics with Follow on click 1 Answer

How to stop waypoints script in another script? 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