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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by jheard901 · Oct 13, 2014 at 03:53 AM · c#movementrelative position

How to move an object relative to another moving object?

Hi, I am making a sign that moves up and down above a player's head while the player is in a zone that pops the sign over their head. My problem is that every time I try to get the sign to move in its local space, it keeps returning to the same position that I have specified in my Update() function - which is an offset from the player's current position. What should I change/use in my code to make this happen?


Ok, I finally got it figured out. It feels like there has to be some better way to do this with Unity, but this is the only working way I figured out to get a sign bouncing over the player's head as they moved around inside a zone.

This is the full source in case anyone else wants to figure out how to do something similar.

 using UnityEngine;
 using System.Collections;
 
 public class TriggerForest : MonoBehaviour {
 
     //private variables
     private bool playerInBounds;
     private bool bCreated; 
     private GameObject signObj;                             //displays the text
     private GameObject floatTextObj;                         //displays instructions to interact
     private static float initDuration = 0.8f;                 //controls duration that sign moves in a direction until switching
     private static float duration = initDuration;
     private static float speed = 0.5f;                         //controls speed that sign moves
     private Vector3 move = new Vector3(0.0f, 0.0f, 0.5f);     //controls direction that sign moves in
     private bool bDone = false;
 
     //public variables
     public GameObject player;
     public GameObject playerPosition;                         //always center on the player
     public GameObject forestSign;
     public GameObject floatText;
     public Vector3 signOffset = new Vector3(0.0f, 0.5f, 3.0f);         //set the default offset for displaying sign above player
     public Vector3 fTextOffset = new Vector3(0.0f, 0.5f, -1.5f);
 
     // Use this for initialization
     void Start () {
 
     }
 
     //check if player is insider trigger
     void OnTriggerEnter(Collider other) {
         if (other.gameObject.tag == "Player") {
             playerInBounds = true;
 
             //Debug.Log(playerInBounds); //test
             Debug.Log ("Entering forest.");
         }
     }
 
     void OnTriggerExit(Collider other) {
         if (other.gameObject.tag == "Player") {
             playerInBounds = false;
 
             //Destroy (other.gameObject); //test
             Debug.Log ("Leaving forest.");
         }
     }
 
     // Update is called once per frame
     void Update () {
 
         //alternative way to reference player
         //var player = GameObject.FindWithTag("Player");
 
         if (playerInBounds) {
 
             //check if user pressed the key to enter level (using e for now)
             //GetKeyDown occurs only once per press, compared to GetKey which continues to occur as long as button is pressed
             if(Input.GetKeyDown(KeyCode.E)) {
 
                 //reference documentation for the line below:    http://docs.unity3d.com/ScriptReference/Application.LoadLevel.html
 
                 //switch the scene to forest level
                 Application.LoadLevel("testSceneLoad");
             }
 
             //ensures we do not continue making objects; also init run once things for sign here
             if(!bCreated) {
 
                 //create objects from prefabs
                 signObj = (GameObject) Instantiate(forestSign, transform.position, transform.rotation);
                 floatTextObj = (GameObject) Instantiate(floatText, transform.position, transform.rotation);
 
                 //set the intial position of sign
                 signObj.transform.position = player.transform.position + signOffset;
 
                 //parent the sign to the player's position; this updates its position automatically
                 signObj.transform.parent = playerPosition.transform;
 
                 bCreated = true;
             }
 
             /* THE SIGN */
             //update position of sign
 
             //moves sign down
             if(!bDone) {
                 if(duration > 0.0f) {
                     
                     signObj.transform.Translate(move * speed * Time.deltaTime, Space.Self);
                     
                     duration -= Time.deltaTime;
                     if(duration <= 0.001f) {
                         duration = initDuration;
                         bDone = true;
                     }
                 }
             }
 
             //moves sign up
             if(bDone) {
                 if(duration > 0.0f) {
                     
                     signObj.transform.Translate(-move * speed * Time.deltaTime, Space.Self);
                     
                     duration -= Time.deltaTime;
                     if(duration <= 0.001f) {
                         duration = initDuration;
                         bDone = false;
                     }
                 }
             }
 
             //update rotation of sign
             var newRot = transform.rotation.eulerAngles;
             newRot.x = 0.0f; newRot.y = 180.0f; newRot.z = 0.0f;
             signObj.transform.rotation = Quaternion.Euler(newRot); //this sits the sign upright
 
 
             /* THE FLOATING TEXT */
             //update position of floating text
             floatTextObj.transform.position = player.transform.position + fTextOffset;
 
             //update rotation of floating text
             var newRot2 = transform.rotation.eulerAngles;
             newRot2.x = 90.0f; newRot2.y = 0.0f; newRot2.z = 0.0f;
             floatTextObj.transform.rotation = Quaternion.Euler(newRot2); //this sits the text upright
         }
         else {
             Destroy(signObj);
             Destroy (floatTextObj);
             bCreated = false;
         }
     }
 }
 
Comment
Add comment · Show 1
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 Fanttum · Oct 13, 2014 at 04:04 AM 0
Share

Is there a reason your not just parenting it to your player?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jkpenner · Oct 13, 2014 at 04:11 AM

So for moving an object relative to another object, one of the best ways of doing that would be to parent it to the object you want it to move relative to. Then to animate or to move it around relative to that object you would adjust the localPosition of the child object. I modified a bit of your code to reflect some of these ideas. (Note: I haven't tested any of this in Unity.)

 if(!bCreated) { 
     signObj = (GameObject) Instantiate(forestSign, transform.position, transform.rotation);
     signObj.transform.parent = player.transform;                    // Parent the signObj to the player
     signObj.transform.localPosition = Vector3.up * textOffset;      // Set the local position of the signObj
     bCreated = true;
 }
  
 // Updates the signObj's position
 signObj.transform.localPosition = Vector3.up * textOffset;
  
 //update rotation of sign
 var newRot = transform.rotation.eulerAngles;
 newRot.x = 0.0f; newRot.y = 180.0f; newRot.z = 0.0f;
 signObj.transform.rotation = Quaternion.Euler(newRot); //this sits the sign upright
  
 // Update the textOffset to move the signObj up and down
 textOffset += moveSpeed * moveDirection * Time.deltaTime;
 
 // Restrict the textOffset between the maxHeight and minHeight
 // and reverse the direction it moves
 if(textOffset > maxHeight) {
     textOffset = maxHeight;
     moveDirection *= -1;
 } else if (textOffset < minHeight) {
     textOffset = minHeight;
     moveDirection *= -1;
 }
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 jheard901 · Oct 13, 2014 at 08:35 PM

Using "signObj.transform.localPosition = Vector3.up * textOffset;" after I have parented the object to the player centers the sign on the player and its position doesn't change from the player's current position. I nearly have it working now though.

So I parented the sign to the player and then I move it in local space which almost does exactly what I am looking to make it do. However, the sign rotates whenever the player rotates, instead of keeping the same position overhead. Is there a way to parent an object to just the position of another object?

Comment
Add comment · Show 1 · 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 KpjComp · Oct 13, 2014 at 09:55 PM 0
Share

I think you could do this by using a dummy object. Assign both the sign & the player to the dummy object. Set transforms on the dummy object, but rotation to the player.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Enemy not moving towards Player 0 Answers

How to allow movement of game object to certain points only 1 Answer

้้้้i want to fixed movement in cube -1 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