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!
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
}
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);
}
}
Its c#, so some translation required.
void Update(){
transform.position = target.position;
transform.rotation = target.rotation;
}
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 <em>= transform.localScale _/ TargetParent.localScale*;*_</em>
}
_nextScale = new Vector3(0, 0, 0);
}
void Update()
{
_localPosition += _lastPosition - transform.position;
_localEulerAngel += _lastEulerAngel - transform.eulerAngles;
for (int i = 0; i < 3; i++)
{
_nextScale = localScale * (lastScale / transform.localScale*);*
}
_localScale = _nextScale;
transform.position = TargetParent.TransformPoint(_localPosition);
transform.eulerAngles = TargetParent.eulerAngles + _localEulerAngel;
for (int i = 0; i < 3; i++)
{
nextScale = TargetParent.localScale * localScale*;*
}
transform.localScale = _nextScale;
UpdateLastTranform();
}
private void UpdateLastTranform()
{
_lastPosition = transform.position;
_lastEulerAngel = transform.eulerAngles;
_lastScale = transform.localScale;
}
}