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 FredVicentin · Oct 31, 2012 at 11:13 AM · 2dmove

2D Character grabing the wall

Hello guys, I'm with problems on my character move. When I jump to one wall my character grab the wall like spider man if I keep the direction buttom pressed, look like the gravity is disabled.

Thats my code:

   using UnityEngine;
   using System.Collections;
     
     [RequireComponent(typeof(Rigidbody))]
     [RequireComponent(typeof(CapsuleCollider))]
     
     public class movePlayer : MonoBehaviour
     {
         public float speed = 10.0f;
         public float gravity = 10.0f;
         public float maxVelocityChange = 10.0f;
         public float jumpHeight = 2.0f;
         private bool grounded = false;
         public float airMove = 5.0f;
         public float distGround = 1;
         public bool checkGround;
 
         void Awake()
         {
             rigidbody.freezeRotation = true;
             rigidbody.useGravity = false;
         }
         void Update()
         {
             
            //Debug.Log("Ground:" + grounded);
            //Debug.Log("Can Jump:" + canJump);
            //Debug.Log(rigidbody.velocity.x);
     
             checkGround = Physics.Raycast(transform.position, -Vector3.up, distGround);
     
     
                 Debug.LogWarning(checkGround);
               
     
                // Calculate how fast we should be moving
                Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
                targetVelocity = transform.TransformDirection(targetVelocity);
                targetVelocity *= speed;
     
                // Apply a force that attempts to reach our target velocity
                Vector3 velocity = rigidbody.velocity;
                Vector3 velocityChange = (targetVelocity - velocity);
                velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);    
                velocityChange.y = 0;
                rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
     
                // Jump
     
                if (checkGround)
                {
                    speed = 10;
                }               
            
            if (checkGround && Input.GetButton("Jump"))
            {    
                rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);        
            }    
            if (!checkGround && rigidbody.velocity.x > 0)
            {
                if (Input.GetKey(KeyCode.A))
                {
     
                    speed = 5;
                }    
            }
            else if (!checkGround && rigidbody.velocity.x < -0)
            {
                if (Input.GetKey(KeyCode.D))
                {
     
                    speed = 5;
                }  
            }
             // We apply gravity manually for more tuning control
            if (!checkGround)
            {
                rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
            }
         }
         float CalculateJumpVerticalSpeed()
         {
             // From the jump height and gravity we deduce the upwards speed 
             // for the character to reach at the apex.
             return Mathf.Sqrt(2 * jumpHeight * gravity);
         } 
     }


Comment
Add comment · Show 2
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 FredVicentin · Oct 31, 2012 at 12:33 PM 0
Share

Please, anybody has some idea ? I really need this

avatar image jethro0610 · Dec 07, 2013 at 04:07 PM 0
Share

If you look in the 2D tutorial you can see the character get stuck also. This is probably a bug in unity and not something wrong with your code

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ziv03 · Dec 07, 2013 at 07:35 PM

You need to add a physics2d material to the wall, setting the friction to 0. steps:

create a physics2d material from the assets and set its friction to 0

select your wall gameobject, and in the boxcollider2d component, set the material to the material you just created

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

11 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

Related Questions

2D Animation does not start 1 Answer

Movement restriction... 1 Answer

Don't move unless in BoxCollider2D (C#) 1 Answer

Applying Force in 2D and calling a function 1 Answer

adjust the player in the ground And movements 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