Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by eric_c · Jul 07, 2012 at 10:56 AM · rotationtransformpositionparentfollow

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eric_c · Jul 08, 2012 at 04:51 AM 0
Share

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?

avatar image nventimiglia · Jul 08, 2012 at 05:48 PM 0
Share

You mean like the smooth follow script included in Standard Assets ?

avatar image fendorio · Mar 06, 2014 at 10:09 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges