- Home /
Following another object's position/rotation like parent/child relationship?
Hi, I've been trying to create a script that attaches itself to an object collided. I've tried many ways, including setting the collider as the parent (which leaded to some scale problems )and creating a fixed joint between the object and the collider (which wasn't plausible because it was expensive to resolve the springy/wiggly connection by turning solver iteration up). So, how do I force an object to follow a specific target's position and rotation without any parent/child relationships or physics involved? Please do help in Javascript, thanks!
Answer by YourGamesBeOver · Mar 06, 2014 at 10:38 PM
pardon my C#, but this should work (I haven't tested it though):
EDIT: found a mistake, fixed it
private Vector3 impactPosOffset;
private Vector3 impactRotOffset;
private Transform objectStuckTo;
private bool stuck = false;
void OnCollisionEnter(Collision collision) {
objectStuckTo = collision.collider.transform; //what did we hit?
impactPosOffset = transform.position-objectStuckTo.position; //where were we relative to it?
impactRotOffset = transform.eulerAngles-objectStuckTo.eulerAngles; //how were we rotated relative to it?
stuck = true; //yeah, we hit something
}
void Update(){
if(!stuck)return;//if we haven't hit anything yet, skip the next part
transform.position = objectStuckTo.position+impactPosOffset; //move to where the stuck object is, plus the offset
transform.eulerAngles = objectStuckTo.eulerAngles+impactRotOffset;//rotate to where the stuck object is, plus the offset
}
Answer by churi24 · Oct 11, 2017 at 09:43 PM
Maybe you could use this...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowParentAsChildSmooth : MonoBehaviour
{
public Transform target;
public float speedPosition = 1f, speedRotation = 1f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.Lerp(transform.position, target.position, speedPosition * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, speedRotation * Time.deltaTime);
}
}
Answer by ReKn · May 18, 2018 at 11:05 AM
Die Frage ist zwar schon alt aber vllt braucht es noch jemand.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveLikeHierarchyChild : MonoBehaviour
{
public Transform TargetParent;
private Vector3 _localPosition;
private Vector3 _localEulerAngel;
private Vector3 _localScale;
private Vector3 _nextScale;
private Vector3 _lastPosition;
private Vector3 _lastEulerAngel;
private Vector3 _lastScale;
private Meta.GrabInteractionMod _grabInteraction;
void Start()
{
UpdateLastTranform();
_localPosition = TargetParent.InverseTransformPoint(transform.position);
_localEulerAngel = transform.eulerAngles - TargetParent.eulerAngles;
for (int i = 0; i < 3; i++)
{
_localScale[i] = transform.localScale[i] / TargetParent.localScale[i];
}
_nextScale = new Vector3(0, 0, 0);
}
void Update()
{
_localPosition += _lastPosition - transform.position;
_localEulerAngel += _lastEulerAngel - transform.eulerAngles;
for (int i = 0; i < 3; i++)
{
_nextScale[i] = _localScale[i] * (_lastScale[i] / transform.localScale[i]);
}
_localScale = _nextScale;
transform.position = TargetParent.TransformPoint(_localPosition);
transform.eulerAngles = TargetParent.eulerAngles + _localEulerAngel;
for (int i = 0; i < 3; i++)
{
_nextScale[i] = TargetParent.localScale[i] * _localScale[i];
}
transform.localScale = _nextScale;
UpdateLastTranform();
}
private void UpdateLastTranform()
{
_lastPosition = transform.position;
_lastEulerAngel = transform.eulerAngles;
_lastScale = transform.localScale;
}
}
Answer by nventimiglia · Jul 07, 2012 at 08:55 PM
Its c#, so some translation required.
void Update(){
transform.position = target.position;
transform.rotation = target.rotation;
}
Sorry, but what I'm trying to achieve is not simply copying the target's transform, but to follow it, like a parent/child relationship. Is there any other ways to do it, someone like adding a certain amount of offset?
You mean like the smooth follow script included in Standard Assets ?
@nventimiglia The follow script in the Standard Assets package does what you posted, just with the ability to set it to facing the following object or not.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073828im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera follow ball along cylinder 1 Answer
Camera rotation around player while following. 6 Answers
An Efficient Way To Follow Transform's Position? 3 Answers
how to stop child object from twitching? 1 Answer
transform.position on basis of rotation 0 Answers