x


Problem of Translation

Hi guys!

I have a little problem with my script which consists to make a kind of plateform.

This script allows an object to move (like Sinusoïdal behavior, or Linear...), and if another object comes on this one, it will have exactly the same movement.

Here is the script which makes the plateform and objects on this one moving Linearly :

float prevX = this.sens * this.speed * Time.deltaTime;

gameObject.transform.Translate(prevX,
                               prevX * this.coeficientA,
                               0, Space.World);


foreach (GameObject it in this.objOnPlateForme)
{
    it.transform.Translate(prevX,
                           prevX * this.coeficientA,
                           0, gameObject.transform);
}

this.sens represents the direction of the plateform movement (-1 or 1), this.speed the speed of the plateform and this.objOnPlateForme the container which contains the objects.

The problem is that the object of the container doesn't move exactly like the plateform. I don't really understand because the value of X and Y are exactly the same, but the objects on it move little bit slower than the plateform.

Do any one know an issue?

more ▼

asked Apr 01 '12 at 02:21 AM

Souss gravatar image

Souss
15 1 1

Is the container a separate object grouped to the platform and does it also have the same script attached to it?

Apr 01 '12 at 04:48 PM garner

Those objects are just other which came on the plateform, like for example a simple cube which fall on it. So, those objects don't have any scripts attached on it.

This is the script that added objects into the container which enter on collision with the plateform and suppressed it when objects are not on it.

void OnCollisionEnter(Collision collider)
{
    if (!this.objOnPlateForme.Contains(collider.gameObject))
    {
        this.objOnPlateForme.Add(collider.gameObject);
    }
    this.fallen = true;
}

void OnCollisionExit(Collision collider)
{
    this.objOnPlateForme.Remove(collider.gameObject);
}

Do not care about the boolean this.fallen, this is for something else.

Apr 01 '12 at 10:01 PM Souss
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Hi!

I finally find what make the objects moving slower. It is because of the gravity of Unity. I must do my own gravity and not use the gravity of unity to have the behavior i was expected.

Thanks for your help!

more ▼

answered Apr 02 '12 at 10:14 PM

Souss gravatar image

Souss
15 1 1

(comments are locked)
10|3000 characters needed characters left

A possible workaround could be to store the difference the platform travels between each tick and offset each object by the distance traveled. It's just an idea I haven't tried it sorry.

float prevX = this.sens * this.speed * Time.deltaTime;


//Store the current position of the platform
Vector3 startPos = gameObject.Transform.position;

//Move the platform
gameObject.transform.Translate(prevX,
                               prevX * this.coeficientA,
                               0, Space.World);


//Store the current position of the platform
Vector3 endPos = gameObject.transform.position;


//The difference between the two vectors
Vector3 offset = new Vector3 (EndPos.x - StartPos.x, EndPos.y - StartPos.y, EndPos.z - StartPos.z);


//Offset the objects
foreach (GameObject it in this.objOnPlateForme)
{
    it.transform.position += offset;
}
more ▼

answered Apr 02 '12 at 02:26 AM

garner gravatar image

garner
120 1

I tried your solution and it wasn't successful unfortunatly. The plateform is still moving a little bit faster than the objects.

But thanks for your attention!

Apr 02 '12 at 07:09 AM Souss
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x223
x2

asked: Apr 01 '12 at 02:21 AM

Seen: 376 times

Last Updated: Apr 02 '12 at 10:14 PM