• 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 Richard-Tec · Apr 14, 2020 at 12:06 PM · collisionmovementphysicscontrollercollision detection

Every time my character hits or touches the walls he gets stuck or acts as if the object the character hit was a magnet.

Hi, I'm new in Unity and I'm creating my first game. When I was testing it, I ran into a problem, every time my character leaned against a wall he got stuck to it, and if I tried to move, he slowly walked as if he was being drawn to the obstacles.

 private void Update()
     {
 
         if (!photonView.IsMine)
         {
             RefreshMultiplayerState();
             return;
         }
 
         //Axis
         float t_hmove = Input.GetAxisRaw("Horizontal");
         float t_vmove = Input.GetAxisRaw("Vertical");
 
 
         //Controls
         bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
         bool jump = Input.GetKeyDown(KeyCode.Space);
         bool crouch = Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl);
         bool pause = Input.GetKeyDown(KeyCode.Escape);
          
 
         //States
         bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.15f, ground);
         bool isJumping = jump && isGrounded;
         bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
         bool isCrouching = crouch && !isSprinting && !isJumping && isGrounded;
 
         //pause
         if (pause)
         {
             GameObject.Find("Pause").GetComponent<Pause>().TogglePause();
         }
 
         if (Pause.paused)
         {
             t_hmove = 0f;
             t_vmove = 0f;
             sprint = false;
             jump = false;
             crouch = false;
             pause = false;
             isGrounded = false;
             isJumping = false;
             isSprinting = false;
             isCrouching = false;
         }
 
         //crouching
         if (isCrouching)
         {
             photonView.RPC("SetCrouch", RpcTarget.All, !crouched);
         }
  
         //Jumping
         if (isJumping)
         {
             if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
             rig.AddForce(Vector3.up * jumpForce);
         }
 
         if (Input.GetKeyDown(KeyCode.U)) TakeDamage(50);
            
         //UI Refreshes
         RefreshHealthBar();
         weapon.RefreshAmmo(ui_ammo);
     }
     // Update is called once per frame
     void FixedUpdate()
     {
 
         if (!photonView.IsMine) return;
 
         //Axis
         float t_hmove = Input.GetAxisRaw("Horizontal");
         float t_vmove = Input.GetAxisRaw("Vertical");
 
         //Controls
         bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
         bool jump = Input.GetKeyDown(KeyCode.Space);
         bool slide = Input.GetKey(KeyCode.LeftControl);
 
         //States
         bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.1f, ground);
         bool isJumping = jump && isGrounded;
         bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
         bool isSliding = isSprinting && slide && !sliding;
 
         //pause
         if (Pause.paused)
         {
             t_hmove = 0f;
             t_vmove = 0f;
             sprint = false;
             jump = false;
             isGrounded = false;
             isJumping = false;
             isSprinting = false;
         }
 
         //Movement
         Vector3 t_direction = Vector3.zero;
         float t_adjustedSpeed = speed;
 
         if (!sliding)
         {
             t_direction = new Vector3(t_hmove, 0, t_vmove);
             t_direction.Normalize();
             t_direction = transform.TransformDirection(t_direction);
 
 
             if (isSprinting)
             {
                 if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
                 t_adjustedSpeed *= sprintModifier;
             }
             else if (crouched)
             {
                 t_adjustedSpeed *= crouchModifier;
             }
         }
         else
         {
             t_direction = slide_dir;
             t_adjustedSpeed *= slideModifier;
             slide_time -= Time.deltaTime;
             if(slide_time <= 0)
             {
                 sliding = false;
                 weaponParentCurrentPos -= Vector3.down * (slideAmount - crouchAmount);
             }
         }
 
         Vector3 t_targetVelocity = t_direction * t_adjustedSpeed * Time.deltaTime;
         t_targetVelocity.y = rig.velocity.y;
         rig.velocity = t_targetVelocity;
 
         //sliding
         if (isSliding)
         {
             sliding = true;
             slide_dir = t_direction;
             slide_time = lengthOfSlide;
             //adjust camera
             weaponParentCurrentPos += Vector3.down * (slideAmount - crouchAmount);
             if (!crouched) photonView.RPC("SetCrouch", RpcTarget.All, true);
         }
 
 
 
         //Field of View and camera stuff
         if (sliding) 
         {
             normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier * 1.2f, Time.deltaTime * 8f);
             normalCam.transform.localPosition = Vector3.Lerp(normalCam.transform.localPosition, origin + Vector3.down * slideAmount, Time.deltaTime * 6f);
         }
         else
         {
             if (isSprinting) { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8f); }
             else { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f); }
 
             if(crouched) normalCam.transform.localPosition = Vector3.Lerp(normalCam.transform.localPosition, origin + Vector3.down * crouchAmount, Time.deltaTime * 6f);
             else normalCam.transform.localPosition = Vector3.Lerp(normalCam.transform.localPosition, origin, Time.deltaTime * 6f);
         }
     }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

277 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 avatar image avatar image 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 hit objects in VR 1 Answer

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

How do I "remove/disable" collision? 3 Answers

Object attached to player doesn't collide 0 Answers

Physic based golf game - ball bouncing off the connection of colliders on flat surface 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