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 oars · Jul 18, 2017 at 08:39 AM · movementlerpsliding

Vector3.Lerp is flinging my character all over the place!

I have a small game where you move a cube around some levels and I want to create an ice hazard that makes the player slide in a direction. I use the following code to do this,

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class iceController : MonoBehaviour {

 private IEnumerator coroutine;
 bool go = false;
 private Vector3 dest;


 private void Update()
 {
     if(go)
     {
         GameObject player = GameObject.Find("cube");
         CubeController cube = player.GetComponent<CubeController>();

        if(cube.lastmov == "up") //slide the player in the direction they are moving
         {
             dest = cube.parent.transform.position + (Vector3.left);
         }
         if (cube.lastmov == "down")
         {
             dest = cube.parent.transform.position + (Vector3.right);
         }
         if (cube.lastmov == "right")
         {
             dest = cube.parent.transform.position + (Vector3.forward);
         }
         if (cube.lastmov == "left")
         {
             dest = cube.parent.transform.position + (Vector3.back);
         }

         cube.parent.transform.position = Vector3.Lerp(cube.parent.transform.position, dest, .2f);
     }

 }

 private void OnTriggerEnter(Collider other)
 {
     GameObject player = GameObject.Find("cube");
     CubeController cube = player.GetComponent<CubeController>();
     go = true;
     cube.input = false; //stop the player from moving while sliding 
 }

 private void OnTriggerExit(Collider other)
 {
     GameObject player = GameObject.Find("cube");
     CubeController cube = player.GetComponent<CubeController>();
     go = false;
     cube.input = true;
 }

}

But for some reason this is giving me inconsictent results. sometimes it does exactly what I want it to do, but other times it will fling the cube across the map violently. Any ideas on why this is happening or how to fix it?

EDIT: It may not specifically have to do with Lerp. I attempted to do the same thing with transform.translate and ran into the same issue, so consider me baffled!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AurimasBlazulionis · Jul 18, 2017 at 09:10 AM

It is most likely because your movement is framerate dependant. The easiest fix would be to change Vector3.Lerp(cube.parent.transform.position, dest, .2f) to Vector3.Lerp(cube.parent.transform.position, dest, .2f * Time.deltaTime), and adjust the speed accordingly.

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 oars · Jul 18, 2017 at 09:48 AM

Thanks for your response! I made the change you suggested and got the speed to where I like but unfortunately this didn't fix the problem. But thanks anyway for your time.

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

96 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

Related Questions

Can my lerp-function be optimized? 1 Answer

Unity 2D Scripted Movement 2 Answers

Smoothly move player over a collection of checkpoints 0 Answers

rotate object by list values 0 Answers

MouseWheel Lerp Smoothing Problem 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