[C#] Unity Precision error?

Well, I’ve found some issues adn i dont know how to get rid of them…

The case is, I have 4 objects (in my case Wheels). They are positioned around one bigger object (in my case Chassis)
something like this:

Now I want to add some symmetrical offset functionality like this:

  Vector3 offset = new Vector3(-.10, 0, 0);
  object.offset.x =  object.offset.x * Mathf.Sign(object.localPosition.x);

The problem i found, is that when i do my symetrical code in Start() then it does not apply to all objects with negative positions.
For example, object2 got the same offset as object1 and object3. But thats not right.

Another thing I found, when I tried to add my symmetrical code in FixedUpdate(), all objects with negative positions started to flicker it’s sign, so the object pretty much jumped aruond the screen.

It seems that my code is not good. So is there a workaround?
Thanks for your time!

		if (object.localPosition.x<0)
			offset.x = -offset.x;

I tried this and this also flickers.

but if the i change < to > it works the opsite way, so what’s up with negative valeus in unity?

edit:
…nah while update, it flickers the oposite way…

so this is tottaly normal?

You haven’t included enough code to replicate your behavior, but you can be sure there is nothing special about negative numbers in unity.There are some 20 million devs using it (at least if you believe Unite keynote), I’m sure a few of them have used negative numbers before.

The first thing that springs to mind with the limited information you have provided is that you refer to position in your assignment example then localPosition in your offset code.

Well…

This code should give you the exact same error:

using UnityEngine;
using System.Collections;

public class Symmetric : MonoBehaviour
{
	public GameObject[] objects;
	public Vector3 offset = new Vector3(-.10,0,0);
	public bool update = true;
	
	void Start ()
	{		
		foreach(GameObject o in objects)
		{		
			offset.x = -offset.x*Mathf.Sign(o.transform.localPosition.x);
			o.transform.localPosition += offset;
		}
	}
	
	void Update()
	{		
    	if(update)
		{
			foreach(GameObject o in objects)
			{		
				offset.x = -offset.x*Mathf.Sign(o.transform.localPosition.x);
				o.transform.localPosition += offset;
			}
		}
	}
}

Save it, create a gameobject, add this cript, make 4 cubes etc., position them as i mentioned before and add those cubes to the list in the script.
Should be that the ones with negative values flicker from -.1 to .1 when in update mode, else, the one wich has -x and -z has also wrong parameters.

Your code explicitly updates back and forth.

Your algorithm boils down to:

x… = x + -kx/|x|

What do you expect your equation to result in?

Hi, i was expecting to flip the X position, where am i going wrong?

you say:

yet you just said you want them to flip.

Which is it? Are they supposed to flip around or not?

Easier question, what do you want to do. Forget your weird code, and just say what you want to do. What is a “symmetrical offset”.

I’m going to guess that a symmetrical offset is you want to move the positions in a certain direction symmetrically. For instance the passenger side tires move outward/right and the driver side move outward/left.

Here’s the thing about symmetry… there’s something called a ‘line of symmetry’. You don’t define your line of symmetry, so how can you offset anything symmetrically with out knowing to what it is symmetrical?

Well I want objects that have position of ( -1, 0, 0) to have position ( 1, 0, 0 ) and stay there. my code flickers them, switching from -1 to 1 and then back to -1 and it never stops. Thats the problem i’m facing.

Hmm a line of symmetry? Shouldnt a value of 0 be a good “line of symmetry”?
I mean I just want the object to be on the oposite side identically.

I thought fliping the sign would simply do this, but when negative values came to play, it started to flicker instead of a constant flip.

You’re doing this in Update, which means it will be executed every frame. You’re essentially changing -x to x, and then next frame changing x to -x. This is happening so fast it creates the flickering effect. You just need to perform this calculation once to flip them.

well i tried this before update, and it only worked for the first one…
also when i set teh code the other way, forcing it with If statement to be the oposite, it still flickers…