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 /
avatar image
0
Question by Fazani · Nov 16, 2019 at 08:59 PM · collision2d-platformermovement script

Problem with 2D movement

Good night,

Before I start, I'm sorry for my English.

I have problems with a movement script for a 2D game. In general the script works fine, but sometimes the player stops walking as if colliding with something in front of it (although he passed the place without problems before).

The scripts

Player:

     void Update()
     {
         Vector2 directions = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
         movement_control.movement(directions, isGrounded());
     }
 
     bool isGrounded()
     {
         Vector2 position = transform.position;
         Vector2 direction = Vector2.down;
         float distance = collider.size[1]/2 + 0.1f;
         RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, groundLayer);
         return hit.collider != null;
     }

Movement:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     public bool bloq_movement;
     public bool is_grounded;
 
     public int current_direction_x;
     public int current_direction_y;
 
     public float direction_x;
     public float direction_y;
     public float jump_force;
     public float vertical_speed;
     public float horizontal_speed;
     public float vertical_max_speed;
     public float horizontal_max_speed;
     public float vertical_acceleration;
     public float horizontal_acceleration;
     public float vertical_bonus;
     public float horizontal_bonus;
     public float gravity;
     public float friction;
 
     public bool movement(Vector2 direction, bool is_grounded)
     {
         Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
 
         setDirectionX(direction[0]);
         setDirectionY(direction[1]);
         this.is_grounded = is_grounded;
 
         if (rigidbody == null) {
             return false;
         }
         
         Vector2 next_point = getMovement();
         rigidbody.velocity = next_point;
         return true;
     }
 
     public Vector2 getMovement()
     {
         if (direction_x == 0)
         {
             addHorizontalSpeed(friction * -current_direction_x);
         } else
         {
             addHorizontalSpeed(horizontal_acceleration * current_direction_x);
         }
 
         if (!is_grounded)
         {
             addVerticalSpeed(-gravity);
         }
         else if (direction_y == 1 && is_grounded)
         {
             addVerticalSpeed(jump_force);
         } else
         {
             vertical_speed = 0;
         }
 
         return new Vector2(horizontal_speed + horizontal_bonus, vertical_speed + vertical_bonus);
     }
 
     public void setDirectionX(float direction)
     {
         if (direction != 0) {
             direction_x = direction > 0 ? 1 : -1;
             current_direction_x = direction > 0 ? 1 : -1;
         }
         else
         {
             direction_x = 0;
         }
     }
 
     public void setDirectionY(float direction)
     {
         if (direction != 0) {
             direction_y = direction > 0 ? 1 : -1;
             current_direction_y = direction > 0 ? 1 : -1;
         }
         else
         {
             direction_y = 0;
         }
     }
 
     public void addHorizontalSpeed(float speed)
     {
         if ((Math.Abs(horizontal_speed) + Math.Abs(speed)) >= horizontal_acceleration)
         {
             horizontal_speed += speed;
         }
         else
         {
             horizontal_speed = 0;
         }
         horizontal_speed = horizontal_speed > 0 ? Math.Min(horizontal_speed, horizontal_max_speed) : Math.Max(horizontal_speed, -horizontal_max_speed);
     }
 
     public void addVerticalSpeed(float speed)
     {
         vertical_speed += speed;
         vertical_speed = vertical_speed > 0 ? Math.Min(vertical_speed, vertical_max_speed) : Math.Max(vertical_speed, -vertical_max_speed);
     }
 
 }
 

Components:

alt text

An example of the error, the player (green square) sometimes stops in the middle of the platform:

alt text

Thanks!

20191116-030547.gif (85.6 kB)
sem-titulo.png (29.6 kB)
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 lgarczyn · Nov 18, 2019 at 01:07 AM 0
Share

I'm not sure what could cause this, but your $$anonymous$$/max lines could be simplified with the $$anonymous$$athf.Clamp function, or the Vector2 function Clamp$$anonymous$$agnitude

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

183 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

Related Questions

Diagonal collision script not working (Unity + Orthello, C#) 1 Answer

Glitchy collisions 0 Answers

My player goes straight through my colider 3 Answers

Platformer crates(boxes) collision bug 1 Answer

How to Apply Animations to Script 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