- Home /
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
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.
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.
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 :)
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.
Yes, you're right! I think I was a little bit confused. It works :) Thanks a lot!
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.
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
With Vector3.$$anonymous$$oveTowards I used 0.1f * Time.deltaTime and the velocity worked how I wonted.
Thanks :)
Your answer
Follow this Question
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