• 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
Question by martipello · May 09, 2017 at 05:20 AM · transformcoroutinelerpenumerate

bounce player back to position while jumping

ive got a player that bounces off an object, my map is a tile structure and when the player bounces off an object he gets pushed back 2 tiles this works fine except when i'm jumping my jump function is an enumerator that lerps the player if he starts at 0,0,1 then it will lerp to 0,1,2 then lerps the player again to 0,0,3 so it travels up and down over two tiles my floor is tagged to let me know when he is on it, if i jump from one tile in front of a bouncer the whole thing works perfectly i bounce off the object and the player is put on the ground as expected however if i jump from 2 tiles in front of the bouncer i land on the bouncer and this pushes my player back the 2 tiles but up higher somewhere around 0,2,0 (assuming the bouncer is at 0,0,2) I'm not using any force or velocity just transform can anyone help?

first here is my jump coroutine

 IEnumerator Jump()
 {
     isMoving = true;
     isGrounded = false;

     StartPosition = transform.position;

     if (Facing == 0)
     {
         EndPosition = transform.position - (new Vector3(0.0f, -2.0f, GridSize));
     }

     if (Facing == 1)
     {
         EndPosition = transform.position - (new Vector3(GridSize, -2.0f, 0.0f));
     }

     if (Facing == 2)
     {
         EndPosition = transform.position - (new Vector3(0.0f, -2.0f, -GridSize));
     }

     if (Facing == 3)
     {
         EndPosition = transform.position - (new Vector3(-GridSize , -2.0f, 0.0f));
     }

     t = 0.0f;
     jumpSource.PlayOneShot(jumpNoise, 1f);


     while (t < 1.0)
     {
         t += Time.deltaTime * (WalkSpeed / GridSize);
         transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
         yield return new WaitForSeconds(0);
     }

     if (!isGrounded)
     {
         StartPosition = transform.position;

         if (Facing == 0)
         {
             EndPosition = transform.position - (new Vector3(0.0f, 2.0f, GridSize));
         }

         if (Facing == 1)
         {
             EndPosition = transform.position - (new Vector3(GridSize, 2.0f, 0.0f));
         }

         if (Facing == 2)
         {
             EndPosition = transform.position - (new Vector3(0.0f, 2.0f, -GridSize));
         }

         if (Facing == 3)
         {
             EndPosition = transform.position - (new Vector3(-GridSize, 2.0f, 0.0f));
         }

         t = 0.0f;

         while (t < 1.0)
         {
             t += Time.deltaTime * (WalkSpeed / GridSize);
             transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
             yield return new WaitForSeconds(0);
         }
     }
     


     isMoving = false;
     turnRight = false;
     turnLeft = false;
     jumping = false;
     //isGrounded is true on contact with the floor
 }

here is my goBackwards coroutine it is called when i bounce

  IEnumerator GoBackward()
 {
     isMoving = true;

         if (Facing == 0)
         {
             EndPosition = StartPosition - (new Vector3(0.0f, 0.0f, -GridSize));
         }

         if (Facing == 1)
         {
             EndPosition = StartPosition - (new Vector3(-GridSize, 0.0f, 0.0f));
         }

         if (Facing == 2)
         {
             EndPosition = StartPosition - (new Vector3(0.0f, 0.0f, GridSize));
         }

         if (Facing == 3)
         {
             EndPosition = StartPosition - (new Vector3(GridSize, 0.0f, 0.0f));
         }

     t = 0.0f;

     while (t < 1.0)
     {
         t += Time.deltaTime * (WalkSpeed / GridSize);
         transform.position = Vector3.Lerp(transform.position, EndPosition, t);
         yield return new WaitForSeconds(0);
     }

     isMoving = false;
 }

i think i need to check inside the goBackwards coroutine to see the player was jumping or !isGrounded but it could be deeper with me using coroutines any help on this would be greatly appreciated

Comment

People who like this

0 Show 0
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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

78 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

Related Questions

Why is this coroutine only firing once? 3 Answers

Static Coroutine being called endlessly 2 Answers

Smooth damp the lowpass frequency in coroutine: value only changing in 1 frame 1 Answer

[HELP] How to make camera smooth change position while follow object? 1 Answer

code using lerp with input 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