Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by gilles14 · Mar 25, 2019 at 08:56 AM · instantiateunity 2dcoordinates

Why coordinates of instantiated object doesn't change?

Hello, I'm new to Unity and I'd like to get any help with my issue.

I have a gnome prefab object and I want to get coordinate of this object when gnome touch other objects in the scene. But coordinates of gnome are always the same as starting point. Here is how I create gnome in the scene from prefab

 GameObject newGnome = (GameObject) Instantiate (gnomePrefab, startingPoint.transform.position, Quaternion.identity);

When gnome touch some trap, I want to get coordinates of this.

 Debug.Log(newGnome.transform.position.y);

But I always get the same starting point coordinates. Why? Here is gnome prefab settings alt text

UPDATE1 Here is the script SignalOnTouch. I attach this script to every trap.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 
 [RequireComponent (typeof (Collider2D))]
 public class SignalOnTouch : MonoBehaviour {
     public UnityEvent onTouch;
     public bool playAudio;
 
     private void OnCollisionEnter2D (Collision2D other) {
         SendSignal (other.gameObject);
     }
 
     private void OnTriggerEnter2D (Collider2D other) {
         SendSignal (other.gameObject);
     }
 
     public void SendSignal (GameObject gameObject) {
         if (gameObject.tag == "Player") {
             onTouch.Invoke ();
         }
     }
 }

And in Unity I setup events which I want to call on touch alt text

Here is the code of GameManager script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class GameManager : Singleton<GameManager> {
     public GameObject startingPoint;
     public GameObject gnomePrefab;
     private GameObject newGnome;
 
     void Start () {
         Reset ();
     }
 
     void Update () {
     }
 
     public void Reset () {
         CreateNewGnome ();
         Time.timeScale = 1.0f;
     }
 
     void CreateNewGnome () {
         RemoveGnome ();
         newGnome = (GameObject) Instantiate (gnomePrefab, 
              startingPoint.transform.position, Quaternion.identity);
     }
 
     void KillGnome (Gnome.DamageType damageType) {
         Debug.Log(newGnome.transform.position.y);
     }
 
     public void TrapTouched () {
         KillGnome (Gnome.DamageType.Slicing);
     }
 }

Gnome is connected to rope. On press down button, rope lenth increased and gnome moved down. Here is the full source of RopeScript

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RopeScript : MonoBehaviour {
 
     public GameObject ropeSegmentPrefab;
     List<GameObject> ropeSegments = new List<GameObject> ();
 
     public bool isIncreasing { get; set; }
     public bool isDecreasing { get; set; }
 
     public Rigidbody2D connectedObject;
     public float maxRopeSegmentLength = 1.0f;
     public float ropeSpeed = 4.0f;
     LineRenderer lineRenderer;
 
     // Start is called before the first frame update
     void Start () {
         lineRenderer = GetComponent<LineRenderer> ();
         ResetLength (); // TODO remove ?
     }
 
     void Update () {
 
         GameObject topSegment = ropeSegments[0];
         SpringJoint2D topSegmentJoint = topSegment.GetComponent<SpringJoint2D> ();
         if (isIncreasing) {
             if (topSegmentJoint.distance >= maxRopeSegmentLength) {
                 CreateRopeSegment ();
             } else {
                 topSegmentJoint.distance += ropeSpeed * Time.deltaTime;
             }
         }
         if (isDecreasing) {
             if (topSegmentJoint.distance <= 0.005f) {
                 RemoveRopeSegment ();
             } else {
                 topSegmentJoint.distance -= ropeSpeed *
                     Time.deltaTime;
             }
         }
         if (lineRenderer != null) {
 
             lineRenderer.positionCount = ropeSegments.Count + 2;
             lineRenderer.SetPosition (0, this.transform.position);
 
             for (int i = 0; i < ropeSegments.Count; i++) {
                 lineRenderer.SetPosition (i + 1,
                     ropeSegments[i].transform.position);
             }
 
             SpringJoint2D connectedObjectJoint = connectedObject.GetComponent<SpringJoint2D> ();
             lineRenderer.SetPosition (ropeSegments.Count + 1, connectedObject.transform.TransformPoint (connectedObjectJoint.anchor));
         }
     }
 
     public void ResetLength () {
         foreach (GameObject segment in ropeSegments) {
             Destroy (segment);
         }
         ropeSegments = new List<GameObject> ();
         isDecreasing = false;
         isIncreasing = false;
         CreateRopeSegment ();
     }
 
     void CreateRopeSegment () {
 
         GameObject segment = (GameObject) Instantiate (ropeSegmentPrefab, this.transform.position, Quaternion.identity);
 
         segment.transform.SetParent (this.transform, true);
 
         Rigidbody2D segmentBody = segment.GetComponent<Rigidbody2D> ();
         SpringJoint2D segmentJoint = segment.GetComponent<SpringJoint2D> ();
 
         if (segmentBody == null || segmentJoint == null) {
             Debug.LogError ("Rope segment body prefab has no " + "Rigidbody2D and/or SpringJoint2D!");
             return;
         }
 
         ropeSegments.Insert (0, segment);
 
         if (ropeSegments.Count == 1) {
 
             SpringJoint2D connectedObjectJoint = connectedObject.GetComponent<SpringJoint2D> ();
             connectedObjectJoint.connectedBody = segmentBody;
             connectedObjectJoint.distance = 0.1f;
 
             segmentJoint.distance = maxRopeSegmentLength;
         } else {
 
             GameObject nextSegment = ropeSegments[1];
 
             SpringJoint2D nextSegmentJoint = nextSegment.GetComponent<SpringJoint2D> ();
             nextSegmentJoint.connectedBody = segmentBody;
             segmentJoint.distance = 0.0f;
         }
         segmentJoint.connectedBody = this.GetComponent<Rigidbody2D> ();
     }
 
     void RemoveRopeSegment () {
         if (ropeSegments.Count < 2) {
             return;
         }
 
         GameObject topSegment = ropeSegments[0];
         GameObject nextSegment = ropeSegments[1];
 
         SpringJoint2D nextSegmentJoint =
             nextSegment.GetComponent<SpringJoint2D> ();
         nextSegmentJoint.connectedBody =
             this.GetComponent<Rigidbody2D> ();
 
         ropeSegments.RemoveAt (0);
         Destroy (topSegment);
     }
 }

screenshot-1.png (287.9 kB)
screenshot-4.png (475.2 kB)
Comment
Add comment · Show 14
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 xxmariofer · Mar 25, 2019 at 09:16 AM 0
Share

hmmmm, can yuou share the full code? since there is something weird in the way you are doing it, where do you have the debug line, inside the oncollision event? if thats the case i would bet you have the GameObject newGnome delcared twice,

avatar image highpockets · Mar 25, 2019 at 09:18 AM 0
Share

We need a bit more info. How do you move the gnome? Share some more script.

avatar image gilles14 · Mar 25, 2019 at 09:43 AM 0
Share

@xxmariofer @highpockets added more code

avatar image xxmariofer gilles14 · Mar 25, 2019 at 10:22 AM 0
Share

can you log in the update the newGnome positiion and check if its being updated? is the gnome child of the rope?

avatar image gilles14 xxmariofer · Mar 25, 2019 at 02:27 PM 0
Share

It always the same -0.62 gnome is not child of the rope, should it be child? rope and gnome connected with SpringJoint2D

Show more comments
avatar image Bonfire-Boy · Mar 25, 2019 at 11:33 AM 1
Share

In the Game$$anonymous$$anager class you call a function RemoveGnome which doesn't appear to exist.

avatar image highpockets Bonfire-Boy · Mar 25, 2019 at 01:11 PM 1
Share

@Bonfire-Boy but his Game$$anonymous$$anager class inherits from his Singleton class which likely contains that function. Anyhow, I don't see where the problem lies going off what you provided, we're still missing something here, but I'm with @xxmariofer, you should check if the position is updating each frame

avatar image Bonfire-Boy highpockets · Mar 25, 2019 at 03:04 PM 0
Share

@highpockets Yes of course, silly of me. What self-respecting Singleton base class would be without a RemoveGnome function ;)

Show more comments
avatar image gilles14 Bonfire-Boy · Mar 25, 2019 at 02:29 PM 0
Share

I cleaned up code for you from methods which doesn't impact on movement and on appearing gnome in the scene. I just forgot to remove here

avatar image Bonfire-Boy · Mar 25, 2019 at 03:11 PM 1
Share

I think you need more informative comments. Just logging a position isn't telling you what you need to know. For example if you change...

 Debug.Log(newGnome.transform.position.y);

...to...

 Debug.LogFormat(this, "Gnome {0}-{1} is at {2}", newGnome.transform.PathInHierarchy(), gameObject.GetInstanceID(),  newGnome.transform.position.y);

...then you'll be able to see for yourself if you've got multiple gnomes

If you don't already have a hierarchy-path function, you'll also need this for the above...

     public static class TransformExtensions
     {
         public static string GetPathInHierarchy(this Transform t)
         {
             Transform parent = t.parent;
             if (parent == null)
             {
                 return t.name;
             }
             return parent.GetPathInHierarchy() + "/" + t.name;
         }
 }    

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JonPQ · Mar 25, 2019 at 03:57 PM

With animated objects... you'll need to check which bone in the hierarchy the collider(s) is attached to. Animations are usually delivered in 2 ways. 1) walking and running animations... dont move the character.... and code moves the root. 2) the animation moves a child object away from the root, but the root stays still. version 2 can cause collision problems like you are describing...

If collider is on the root of the object... perhaps the gnome is animated away from the root position with other bones as in option 2 above.... if so, the collider (on the root) won't move with the gnome.

So check that first, also check how the root moves compared with the gnome in scene view. If so, then attach to hips or something further down the hierarchy.

You could also put simple box and sphere colliders on key bones in the gnome to use as triggers for when gnome touches something. (if you need to get more accurate than capsule collider)

If your collisions are working, but the co-ordinates are static, keep a reference to a transform on the gnome such as abdomen or hips, not the object root. then use the world position of that object ( not local) after detecting the collision. Another option, if your code is using functions.... OnCollisionEnter... that passes in the collision information to you, so you can use collision.point which is exactly the collision point (or array of collision points)

Comment
Add comment · Show 2 · 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 xxmariofer · Mar 25, 2019 at 04:11 PM 0
Share

hello, the quesstion is already answered, the answer is posted in the commments, @gilles14 please convert your cimment to an answer and mark it as correct

avatar image gilles14 · Mar 25, 2019 at 04:53 PM 0
Share

thank you for so detailed answer, I already fixed the problem. But thanks anyway

avatar image
0

Answer by gilles14 · Mar 25, 2019 at 03:17 PM

finally, I got it. Rope with gnome was connected by body of the gnome in GameManeger script

 public RopeScript rope;
 rope.connectedObject = currentGnome.ropeBody;

Coordinates of ropeBody are correct. Thank you guys for suggestions and help! @xxmariofer @highpockets @Bonfire-Boy

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

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

131 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unexpected symbol 'Instantiate' [SOLVED] 1 Answer

My objects instantiate at a strange z coordinate 0 Answers

Different child position when parent gameobject selected 0 Answers

Script inside instantiated prefab run lag behind 1 Answer

Why is my default Z set as -4000? 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