Hi I am having trouble figuring out how I can clamp and move an object position on its local transform.
If this info matters I am setting the objects transform rotation to another object that is a child of a different object.
Below this works for local space. I feel like I am missing 1 step.
this.transform.rotation = SlidePivot.transform.rotation;
float x = SlidePivot.transform.position.x;
float y = SlidePivot.transform.position.y;
float z = SlidePivot.transform.position.z;
x = Mathf.Clamp(this.transform.position.x, x - .01f, x + .01f);
this.transform.position = new Vector3(x, y, z);
Any thoughts?
Thanks
It’s unclear to me what is being clamped to what. Is it possible that you intend to use the transform.localPosition property instead? That is the locally-rotated relative position of an object to its parent, but obviously not to ANOTHER object that it isn’t parented to.
so I have object “A”. I am taking object “B” position and rotation and setting those to A. I want to clamp in this case object A x.position based on the x.position of object B. This way I can move object A on the xAxis within those constraints. Right now I am able to get this to work but object A is clamping in the world rather than local position.
When I have attempted to use local position Object A starts flickering between two points in space.
Hopefully I explained this better.
This is something I have come up with, however the syntax is very annoying and I am curious if there is an easier way to write this.
//Clamp
float JC_minObjX;
float JC_maxObjX;
float JC_ObjX;
float JC_Objy;
float JC_Objz;
public GameObject Leader;
public GameObject Follower;
bool Toggle = false;
void NeedleGrab()
{
JC_ObjX = transform.InverseTransformVector(transform.position).x;
JC_Objy = transform.InverseTransformVector(transform.position).y;
JC_Objz = transform.InverseTransformVector(transform.position).z;
JC_minObjX = JC_ObjX - 1;
JC_maxObjX = JC_ObjX;
}
void Update()
{
Follower.transform.eulerAngles = Leader.transform.eulerAngles;
if (Input.GetKeyDown(KeyCode.I))
{
NeedleGrab();
Toggle = true;
}
if (Toggle == true)
{
float clamp = Mathf.Clamp(transform.InverseTransformVector(transform.position).x, JC_minObjX, JC_maxObjX);
Vector3 noclamp = new Vector3(clamp, JC_Objy, JC_Objz);
Follower.transform.position = Follower.transform.transform.TransformVector(noclamp);
}
}
}
I am not sure yet if this will work with what I am attempting to do yet. I should know relatively soon though.
I am a little confused about inverse transform vector and local position.