Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Zet26 · Mar 04, 2021 at 03:31 PM · cameramovementcamera-movement

Player Movement using lerp

I am trying to make a game in which you have to cheat in order to pass the exam, and i want to be able to move around your desk (with W, A, S, D of course) and when I release the key to come back to chair. I managed to do that using lerp, but it looks weird when it reach the max distance. I will show you the movement script and the lerp script, and a video to fully understand what I mean. The problem is that it looks very weird. My question is, is there anyway to avoid that glitchy look? Please note: I am new to this, this is my second game. The link to the video: https://www.youtube.com/watch?v=3gzHiGyK5l4&feature=youtu.be&ab_channel=ZET

player-movement.png (35.3 kB)
lerp.png (31.1 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Mar 04, 2021 at 05:38 PM

Do you want that max distance still? If not just turn of the lerping while you are moving. If you do, just move the Lerping to Update. The that jittering is caused by desync between the movement in update and the movement in fixed update.

Comment
Add comment · Show 3 · 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 Zet26 · Mar 04, 2021 at 06:58 PM 0
Share

I moved the Lerp to update, and it looks the same. But how do I turn off the lerp while moving? I tried to disable it when hitting w,a,s or d, but I was not really able to do it.

avatar image unity_ek98vnTRplGj8Q Zet26 · Mar 04, 2021 at 07:34 PM 0
Share

Set a bool in your movement script to true whenever you are moving from an input, then get a reference to your character movement in your Lerp script and just return out of the update method if that bool is true;

avatar image Zet26 unity_ek98vnTRplGj8Q · Mar 04, 2021 at 09:12 PM 0
Share

Oke, thanks. I will try tomorrow!

avatar image
0

Answer by UnityM0nk3y · Mar 04, 2021 at 10:11 PM

I made you a little "AI" code, that uses a "Coroutine" to move back. Switch off all your codes, only add mine to your player, and assign the "Start Position && "Speed in the inspector. I never worked in Unity3D, but tried to convert it to "3D" world space. :)

Maybe this is too advanced for what you want? :D

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerController : MonoBehaviour
 {
     public float speed;
     private float modifiedSpeed; //the speed we will modify to move back
     public Transform startPosition; //assign in inspector -> this is most likely your chair
     bool stopUserInput; // a bool to prevent any other input, and wait till the user returns
 
     bool horizontalMove; //used in the co-routine to move you
     bool veriticalMove;  //used in the co-routine to move you
 
     private Vector3 positionFromStart; //used in the co-routine to determine your orientation
     private Vector3 move;
 
     void Start()
     {
         transform.position = startPosition.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
 
         move = transform.right * x + transform.forward * z;
 
         if (!stopUserInput) //If we are allowed to move
         {
             //If we are not at our start pos yet
             if (move == new Vector3(0,0,0) && transform.position != startPosition.position) 
             {
                 stopUserInput = true;
                 StartCoroutine(walkBackToStartPos());
             }
             else
             {
                 transform.Translate(move * speed * Time.deltaTime); //else move normally
             }
         }
 
         #region Movement for Co-Routine
         if (horizontalMove) //move with the set speed in the co-routine (walkBackToStartPos)
         {
             transform.Translate(transform.right * modifiedSpeed * Time.deltaTime);
         }
         if (veriticalMove)
         {
             transform.Translate(transform.forward * modifiedSpeed * Time.deltaTime);
         }
         #endregion
     }
 
 
     IEnumerator walkBackToStartPos()
     {
         if (transform.position.z < startPosition.position.z) //move up
         {
 
             modifiedSpeed = speed;
             veriticalMove = true;
             yield return new WaitUntil(() => transform.position.z >= startPosition.position.z);
             veriticalMove = false;
             if (transform.position.x < startPosition.position.x) //moveRight
             {
                 modifiedSpeed = speed;
                 horizontalMove = true;
                 yield return new WaitUntil(() => transform.position.x >= startPosition.position.x);
                 horizontalMove = false;
                 transform.position = startPosition.position;
                 print("Moved Back");
                 stopUserInput = false;
             }
             else //moveLeft
             {
                 modifiedSpeed = -speed;
                 horizontalMove = true;
                 yield return new WaitUntil(() => transform.position.x <= startPosition.position.x);
                 horizontalMove = false;
                 transform.position = startPosition.position;
                 print("Moved Back");
                 stopUserInput = false;
             }
 
         }
         else //moveDown
         {
             modifiedSpeed = -speed;
             veriticalMove = true;
             yield return new WaitUntil(() => transform.position.z <= startPosition.position.z);
             veriticalMove = false;
             if (transform.position.x < startPosition.position.x) //moveRight
             {
                 modifiedSpeed = speed;
                 horizontalMove = true;
                 yield return new WaitUntil(() => transform.position.x >= startPosition.position.x);
                 horizontalMove = false;
                 transform.position = startPosition.position;
                 print("Moved Back");
                 stopUserInput = false;
             }
             else //moveLeft
             {
                 modifiedSpeed = -speed;
                 horizontalMove = true;
                 yield return new WaitUntil(() => transform.position.x <= startPosition.position.x);
                 horizontalMove = false;
                 transform.position = startPosition.position;
                 print("Moved Back");
                 stopUserInput = false;
             }
         }
 
     }
 
 }



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
avatar image
0

Answer by Zet26 · Mar 05, 2021 at 09:20 AM

I did something else, I just placed some cubes in order to prevent me from going to far. Thank you for your support!

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

223 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

Related Questions

How to make camera move like it does in the scene viewport? 0 Answers

Regarding transform.position in the roll a ball tutorial 1 Answer

Jittery Movement - specific mechanics in mind 0 Answers

Having a Terrible Stutter on Player Movement in 2D 0 Answers

Camera Movement 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