• 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
Question by zachypin · Jun 23, 2011 at 08:09 PM · animationerrortransformposition

Animation Clobbers Position

So I have an object in my world that has its location based on a tagged object in my world. code:

 var objectToFollow;
 
 var onM = true;
 var on1 = false;
 var on2 = false;
 var on3 = false;
 
 function Update () 
 {
     if(gameObject.Find("localPlayer")==null)
     {}
     else
     {
        //animation code omitted 
        objectToFollow = GameObject.FindWithTag("Player").transform;
         transform.position = objectToFollow.TransformDirection(Vector3.forward * 4) + objectToFollow.position;
         transform.localPosition.y += 2;
         transform.rotation = objectToFollow.rotation * Quaternion.Euler(0,-90,90);
     }
 }

This positions the object in front of my player. I have animations that play when I push the specified buttons. The problem is that when I do the animation the position of the object completely changes and doesn't stay in front of the player. The code for the animations fall where the code above says it was omitted.

//code from animation

 if(onM)
 {
     if(Input.GetKeyDown("o"))
     {
         animation.Play("m-1");
         onM = false;
         on1 = true;
     }
     else if(Input.GetKeyDown("l"))
     {
         animation.Play("m-3");
         onM = false;
         on3 = true;
     }
 }
 else if(on1)
 {
     if(Input.GetKeyDown("o"))
     {
         animation.Play("1-2");
         on1 = false;
         on2 = true;
     }
     else if(Input.GetKeyDown("l"))
     {
         animation.Play("1-m");
         on1 = false;
         onM = true;
     }
 }
 else if(on2)
 {
     if(Input.GetKeyDown("o"))
     {
         animation.Play("2-3");
         on2 = false;
         on3 = true;
     }
     else if(Input.GetKeyDown("l"))
     {
         animation.Play("2-1");
         on2 = false;
         on1 = true;
     }
 }
 else if(on3)
 {
     if(Input.GetKeyDown("o"))
     {
         animation.Play("3-m");
         on3 = false;
         onM = true;
     }
     else if(Input.GetKeyDown("l"))
     {
         animation.Play("3-2");
         on3 = false;
         on2 = true;
     }
 }

When this object is parented to the player rather than transformed based on the tag the objects animation works correctly, however that's not what I want to do due to other factors. Any ideas?

Comment

People who like this

0 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 zachypin · Jun 23, 2011 at 08:20 PM 0
Share

Also. this code is setup on the parent because that is where the animation occurs. This is on a menu type gameObejct. So the hierarchy is like 1)Menu. 2) Main 2)option1 2)option2 2)option3 2) buttons 3)downbutton 3)upbutton 1 is the parent. 2 is under menu. 3 is under buttons. Not sure if it matters that the animations being played are on the parent item.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by zachypin · Jun 27, 2011 at 03:46 PM

I think the problem is that I am attaching the object to the Player via the parent of the gameobject. At the same time the animation that is running is on the parent of the gameobject.

To solve this I am currently making a parent object that is only holding the attachment code with no animations. Then a child of that will hold all of the animations, and the child of that will hold the onclick functions that say when interact with object call parent and do animation. This will only work if my assumption is correct that transform.parent only calls the direct parent and not the overarching parent. I will let you know.

Comment
alone1992

People who like this

1 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 zachypin · Jun 27, 2011 at 05:05 PM 0
Share

worked :)

avatar image

Answer by Molix · Jun 24, 2011 at 01:30 AM

It is hard to say without seeing it, but it sounds as if the animation is moving the root of the object in question, probably to some location around (0,0,0). So when it is not attached to the player, it warps to near the origin; when it is attached, it is relative to the player. I'd suggest attaching the animating object in question to a new, dummy object, and then use your code above to position that.

Comment

People who like this

0 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 Molix · Jun 27, 2011 at 03:55 PM 0
Share

Yes, that is what it sounds like. Good luck.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Simple animations - hardcoded or import animation 1 Answer

In animations, is it possible to only add transform values instead of setting it? 2 Answers

Parts of imported blender object are misplaced (when starting game) 0 Answers

Play animation as the value changes from 0 to 1 with changing GameObject's position in unity 0 Answers

transform.position error 1 Answer


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