- Home /
How can I move a corresponding object.
Hi, how can I move an object up and down in correspondence of the another object? Which is the easier way? Someone told me that I can use the same code also for both object, but I tried and this did not work.
Thank you in advance
Off Topic :
@PaxStyle Friendly re$$anonymous$$der, please go through your 98 questions and Accept any correct answers by clicking the tick. When someone has taken the time to help you and the solution has worked, its the polite thing to do :)
Answer by Mediamonkey · Sep 15, 2014 at 01:25 PM
I added this script to the two parts that make up the piston. The hinge for the top of the piston is located on an arm, but is optional for the body of the piston. The sliding top of the piston contains the same script and the lookAtTarget is each others transform.
I added a rotationOffset so both pieces don't need to initially point in the correct direction (forward).
Now if you move the arm, the piston will move correctly. It is possible that it overshoots, but that is the animator's choice.
using UnityEngine;
using System.Collections;
public class SimplePiston : MonoBehaviour {
public Transform hinge;
public Transform lookAtTarget;
public Vector3 rotationOffset;
protected Transform tf;
void Start() {
tf = transform;
}
void Update () {
if (hinge != null) tf.position = hinge.position;
tf.LookAt(lookAtTarget);
tf.Rotate(rotationOffset);
}
}
Answer by Juice-Tin · Jul 04, 2014 at 08:05 PM
Easy solution with perfect outcome:
1. Attach the actual piston to the pink arm (make sure the pistons center is at the tip where it touches the arm)
2. Put script on the piston transform.LookAt(pistonHolder);
3. Put script on that piston holder transform.LookAt(piston);
4. Celebrate the perfect piston mechanics!
This will attach your piston to the arm, and because the piston and piston holder are looking at each other, they will slide into each other perfectly. Note you may need to put them in parent objects to make sure they're aligned properly and facing the right direction.
I tried this, I created 2 empty game object positioned near the piston and the piston holder, and i put them into the new game objects. I put this code for both, as you said, but when i press play, the position are all wrong. Ah, the piston holder should not move, so I don't drag the code into it. is not correct my steps?
using UnityEngine;
using System.Collections;
public class Piston : $$anonymous$$onoBehaviour {
public Transform target;
void Update() {
transform.LookAt(target);
}
}
Your answer
Follow this Question
Related Questions
using Transform on a Object will not move it 100% exactly... 1 Answer
Editor like spore? (move object on other object) C# 0 Answers
What am I doing wrong? 1 Answer
Rotate object with another object 0 Answers
Move another cube with a cube 1 Answer