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 PastorJason · Sep 20, 2020 at 09:54 AM · 2d game2d-platformerjumpingclick to moveside scroller

Can't implement jumping to 2D side-scroller click-to-move.

So I'm trying to make a 2d side-scroller platformer, but instead of using traditional "Left/Right" arrow keys or "A/D", I've created a click to move controller that only moves the player horizontally. However I'm trying to implement a jump mechanic but I cannot seem to get the jump to work right with the click to move controls. Whenever I jump the character seems to have a varied jump velocity every time. Also when I walk off of platforms (to get to another platform below) the character seems to stop midair for a second then actually start to fall and I also think it is because of how my click to move controller is set up. Any suggestions/tips would be greatly appreciated! Here is my movement code:

 public Rigidbody2D rb;

 public float speed = 10f;
 private Vector2 targetPosition;
 private bool isMoving = false;

 //grounded check for jumps
 public float jumpVelocity = 10f;
 private bool isGrounded;
 public Transform groundCheck;
 public float checkRadius;
 public LayerMask whatIsGround;

 private void Update()
 {
     if (Input.GetMouseButton(0))
     {
         SetTargetPosition();
     }

     if (Input.GetKey(KeyCode.LeftShift))
     {
         isMoving = false;
     }

     if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
     {
         rb.velocity = Vector2.up * jumpVelocity * Time.deltaTime;
     }

     if (transform.position.y == targetPosition.y && transform.position.x == targetPosition.x)
     {
         isMoving = false;
     }
 }
 private void FixedUpdate()
 {
     isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

     if (isMoving)
     {
         rb.position = Vector3.MoveTowards(rb.position, targetPosition, speed * Time.deltaTime);
     }
 }

 private void SetTargetPosition()
 {
     targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     targetPosition.y = transform.position.y;

     isMoving = true;
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by PastorJason · Sep 22, 2020 at 07:13 AM

So I figured out the problem with my code. Basically I was moving my rigidbody on both the x and y axis therefore my jump was being run over by the click to move mechanic. So I changed my rigid body to only move on a new Vector2 that was the x value of the click to move and the y value of my current position. All in all I changed line 31 to:


if (rb.position.x == targetPosition.x)


and line 42 to:


rb.position = Vector2.MoveTowards(rb.position, new Vector2(targetPosition.x, rb.position.y), speed*Time.deltaTime);

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

184 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

Related Questions

Long press for charged Jump 1 Answer

Raycast2D not working 1 Answer

Problem with Jump 1 Answer

How do I do a Snappy Jump in 2D with a 2D Jump Script? 0 Answers

Jump only when Jump button is pressed - not held 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