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 rappatic · Jun 28, 2018 at 05:10 PM · 2dcollisionmovement2d-platformer

Moving a player smoothly without breaking collisions?

I'm working on a 2D platformer and currently to move the player I'm simply editing its transform. This is smooth way of moving my character, but unfortunately it doesn't work for my collisions as my player is teleported into blocks and forced back out.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerMovement : MonoBehaviour {
 
     static float speed = 0.07f;
 
     public Transform player;
     public PolygonCollider2D playerCollider;
     public Rigidbody2D playerRigid;
 
     public Transform sightStart;
     public Transform sightEndRight, sightEndLeft;
 
     Vector3 goRight = new Vector2(speed, 0);
     Vector3 goLeft = new Vector2(-speed, 0);
     Vector3 goHome = new Vector3(-18.275f, 8.028f, 0f);
 
     void Update ()
     {
 
         
         if (((Input.GetKey ("a") || Input.GetKey ("left")) && player.position.x > -28.65f)) {
                 player.position += goLeft;
         }
 
 
 
         if (((Input.GetKey ("d") || Input.GetKey ("right")) && player.position.x < 20.22f)) {
                 player.position += goRight;
         }
 
 
 
         if ((Input.GetKeyDown ("w") || Input.GetKeyDown ("up")) && onGround()) {
             playerRigid.AddForce (new Vector2 (0, 4), ForceMode2D.Impulse);
         }
 
         if (player.position.y < -12) {
             player.position = goHome;
         }
 
 
     }
 
 
     bool onGround () {
         GameObject[] solidObjects = GameObject.FindGameObjectsWithTag ("solidObject");
 
         foreach (GameObject solidObject in solidObjects) {
 
             if (playerCollider.IsTouching (solidObject.GetComponent<BoxCollider2D> ())) {
                 return true;
             }
         }
 
         return false;
 
     }
 
 }


Is there a way to move my player so that it retains the smooth movement but collides with object correctly? I'd prefer not to use AddForce() unless it keeps my smooth movement.

Including code would be very helpful as I'm new to Unity.

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 hexagonius · Jun 28, 2018 at 05:22 PM 0
Share

alter the velocity on FixedUpdate and use interpolation mode on the rigidbody

avatar image rappatic hexagonius · Jun 28, 2018 at 05:57 PM 0
Share

It would be great if you could include some code :-)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hectorux · Jun 28, 2018 at 11:03 PM

You could use a character controller component, works similar of the transform, instead you have to reference it and use the method Move(). Also detect collision, but if you want to detect triggers it wont work, althoug you can just put a collider too and will work

Comment
Add comment · Show 2 · 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
avatar image rappatic · Jun 30, 2018 at 08:37 AM 0
Share

It would be great if you could include some code :-) thanks

avatar image hectorux rappatic · Jul 01, 2018 at 07:09 AM 0
Share

CharacterControler cc;

 void Start(){
 
 cc=getComponent<CharacterController>();
 
 }
 
 void Update(){
 
 cc.$$anonymous$$ove(Vector3 direction* speed* Time.detlaTime)
 //It work like a translate but it will collide
 }
avatar image
0

Answer by no00ob · Jul 02, 2018 at 09:28 PM

I'd say this might be what you need: Link also still works on the newest version of unity.

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

248 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

Related Questions

Make characters go through each other 1 Answer

Smooth Collisions 2D 0 Answers

Rigid Body Movement (Mobile Issue) 3 Answers

Projectile Ricochet Issue. Travels along the surface rather than reflecting off of it 2 Answers

Rolling Barrels through Platforms 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