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 ppddgg · Aug 11, 2016 at 07:07 PM · c#lerptranslatemathfclamp

Slide object between two moving points c#

Hi everyone,

sorry for my English, I'm trying to learn it by myself.

I need help to create a c# script to make slide an object between two moving points.

PointA and PointB have attached a script which move them randomly in the scene.

In the image linked below in step 1 you can see the point A, the point B and the spawned object in the starting situation

Image and video hosting by TinyPic

In step two in the image linked above you can see my current situation whit the script below which is attached to spawned object:

 private Transform pointA;
 private Transform pointB;
 
 void Update()
     {
         pointB = GameObject.FindWithTag ("PointB").GetComponent<Transform> (); //Constant search of pointB's position in the scene
         float speed = -0.1f * Time.deltaTime; 
         transform.position  =  pointA.position + (poinA.position - pointB.position) * speed;
 }

If the pointB are moving the spawned object doesn't remains on the line between the two points.

In step 3 in the image you can see what I want: I need that the spawned object face to point B and constantly remains on the line between the two points while moving to poinB.

EDIT: I forgotten to say that the distance between the points is not always the same.

Thank you for any help you give me.

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
4
Best Answer

Answer by LK84 · Aug 11, 2016 at 08:18 PM

1) Don't use GameObject.FindWithTag ("PointB").GetComponent<Transform> (); in the Update() function. Put it in start or make public Transform variables and drag them in the editor on the script

2)Use FixedUpdate() to prevent jitter

3) Use Vector3.Lerp for a smooth movement. Example Code:

 public class slide : MonoBehaviour {
 
     public Transform pointA, pointB;
     float speed;
 
     void FixedUpdate ()
     {
         speed =10*Time.deltaTime;
         transform.position = Vector3.Lerp(pointA.position, pointB.position, speed);
 
     }

By using point A as destination and pointB as target, you ensure that your spawned object remains in one line between the two.

Comment
Add comment · Show 7 · 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 ppddgg · Aug 11, 2016 at 09:44 PM 0
Share

Point B have a script which move it with translate, so it have a constant move. I need FindWithTag in Update to Find the current position of the pointB. If I put the code in start, it found the position of pointB only at start of the scene. Vector3.Lerp, Vector3.$$anonymous$$oveToward are useless, I think. I Need to find Vector3 between the two points and fix the cube on it, near pointA, and than translate the cube on this Vector3 which change position every frame because pointB are moving. But I don't know how :(

Put the code in FixedUpdate is useful. I thought it was only for Rigidbody's movement. Thanks :)

avatar image LK84 · Aug 11, 2016 at 09:55 PM 1
Share

I think you are misunderstanding an important point. GameObject.FindWithTag ("PointB").GetComponent<Transform> (); gets you the Transform Component of the Game Object, and this one is NOT changing at any time. So there is absolutely no need to put it in the Update method, it's only slowing down your game. And yes, you will always get the current position. I reproduced your example and my script works perfectly, so just go ahead and try it out.

avatar image ppddgg LK84 · Aug 11, 2016 at 10:09 PM 0
Share

Yes, you're right! I think I was a little bit confused. It works :) Thanks a lot!

avatar image Bunny83 · Aug 11, 2016 at 10:11 PM 2
Share

Don't use FixedUpdate for things that aren't related to physics. FixedUpdate actually increases jitter. The most smooth movement is achieved by using Update.

Apart from that you use Lerp wrong. The "t" value has to be increased each frame so it goes from 0 to 1. If you use "10*Time.deltaTime" it would stay roughly around "0.6" if the framerate is 60 fps.

avatar image LK84 Bunny83 · Aug 11, 2016 at 10:16 PM 1
Share

Usually I'd agree, but in this particular case using Update() causes jitter (We are kind of implementing our own physics here ;) )Try it out for yourself.

I agree with your second point, but since I don't know what velocity @ppddgg needs i just used his example

avatar image ppddgg LK84 · Aug 11, 2016 at 10:24 PM 0
Share

With Vector3.$$anonymous$$oveTowards I used 0.1f * Time.deltaTime and the velocity worked how I wonted.

Thanks :)

Show more comments

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

205 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

Related Questions

Mathf.Lerp not working 2 Answers

Mathf.Lerp won't stop snapping 2 Answers

Multiple Cars not working 1 Answer

Vector2.Lerp not working properly. Horizontal jittering when not moving horizontally at all! 1 Answer

Distribute terrain in zones 3 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