Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Light997 · Nov 29, 2015 at 04:32 AM · c#animationtransform

Positions changing seemingly randomly

I have made a game prototype that allows you to switch your currently equipped tool in a specific spot, the tool storage. I implemented this function by having an enum with the possible tools that is cycled through. E.g. if you have the scythe equipped and press E, the game destroys the scythe and instantiates a sack of seeds. I have seperate scripts determining which object to instantiate where and with which rotation, the "Save(ToolNameHere)" scripts. They store this info in form of:

Object - GameObject

Position - Vector3

Rotation - Quaternion

They also contain some other script info that isn't important here.

Anyway, the script for the tool station is this: using UnityEngine; using System.Collections;

 public enum CurrentTool
 {
     Sense,
     Samen,
     Pflug,
     None
 }
 
 public class ToolRack : MonoBehaviour {
 
     public CurrentTool currentTool;
 
     void OnTriggerStay (Collider other)
     {
         if (other.CompareTag("Player"))
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 SwapTools(other.gameObject);
             }
         }
     }
 
     void SwapTools(GameObject player)
     {
         ToolManager toolManager = player.GetComponent<ToolManager>();
         CurrentTool playerTool = toolManager.currentTool;
 
         switch (playerTool)
         {
             case CurrentTool.None:
                 toolManager.SwitchTools(CurrentTool.Sense);
                 break;
             case CurrentTool.Sense:
                 toolManager.SwitchTools(CurrentTool.Samen);
                 break;
             case CurrentTool.Samen:
                 toolManager.SwitchTools(CurrentTool.Pflug);
                 break;
             case CurrentTool.Pflug:
                 toolManager.SwitchTools(CurrentTool.None);
                 break;
         }
     }
 }



(If you're wondering why some of it's in German, that's simply because I use the word that pops into my head first[Samen = Seeds, Sense = Scythe, and Pflug = Hoe])

The script that exchanges the tools is this:

 using UnityEngine;
 
 public class ToolManager : MonoBehaviour {
 
     public CurrentTool currentTool;
     private GameObject toolInHand;
     public GameObject player;
     public SaveSense saveSense;
     public SaveSamen saveSamen;
     public SaveHoe saveHoe;
     private SeedingManager seedingManager;
     private Inventory inventory;
     private Ertrag ertrag;
     private SeedSack seedSack;
 
     private GameObject toBeInstantiated;
     private Vector3 transformPosition;
     private Quaternion transformRotation;
 
     private Vector3 zero = new Vector3(0, 0, 0);
     private Quaternion zeror = new Quaternion(0, 0, 0, 0);
 
     private Transform playerPosition;
     private Transform posXLimit;
     private Transform negXLimit;
     private Transform posZLimit;
     private Transform negZLimit;
 
     private string debug;
 
     void Start()
     {
         currentTool = CurrentTool.None;
     }
 
     public void SwitchTools(CurrentTool newTool)
     {
         currentTool = newTool;
         Destroy(toolInHand);
         if(newTool != CurrentTool.None)
         {
             ApplyVariables(newTool);
             toolInHand = (GameObject)Instantiate(toBeInstantiated, transformPosition, transformRotation);
             SetTransformandName(toolInHand, newTool);
             ApplyScripts(newTool);
         } else
         {
             ApplyVariables(CurrentTool.None);
         }
     }
 
     void ApplyVariables(CurrentTool tool)
     {
         switch (tool)
         {
             case CurrentTool.Sense:
                 toBeInstantiated = saveSense.senseObject;
                 transformPosition = saveSense.transformPosition;
                 transformRotation = saveSense.transformRotation;
                 inventory = saveSense.inventory;
                 debug = string.Format("{0},{1},{2}", toBeInstantiated, transformPosition, transformRotation);
                 Debug.Log(debug);
                 break;
             case CurrentTool.Samen:
                 toBeInstantiated = saveSamen.samenObject;
                 transformPosition = saveSamen.transformPosition;
                 transformRotation = saveSamen.transformRotation;
                 seedingManager = saveSamen.seedingManager;
                 playerPosition = saveSamen.playerPosition;
                 posXLimit = saveSamen.posXLimit;
                 negXLimit = saveSamen.negXLimit;
                 posZLimit = saveSamen.posZLimit;
                 negZLimit = saveSamen.negZLimit;
                 debug = string.Format("{0},{1},{2}", toBeInstantiated, transformPosition, transformRotation);
                 Debug.Log(debug);
                 break;
             case CurrentTool.Pflug:
                 toBeInstantiated = saveHoe.hoeObject;
                 transformPosition = saveHoe.transformPosition;
                 transformRotation = saveHoe.transformRotation;
                 debug = string.Format("{0},{1},{2}", toBeInstantiated, transformPosition, transformRotation);
                 Debug.Log(debug);
                 break;
             case CurrentTool.None:
                 toBeInstantiated = null;
                 transformPosition = zero;
                 transformRotation = zeror;
                 debug = string.Format("{0},{1},{2}", toBeInstantiated, transformPosition, transformRotation);
                 Debug.Log(debug);
                 break;
         }
     }
 
     void ApplyScripts(CurrentTool tool)
     {
         if (tool == CurrentTool.Sense)
         {
             ertrag = toolInHand.GetComponent<Ertrag>();
             ertrag.player = inventory;
         }
         else if (tool == CurrentTool.Samen)
         {
             seedSack = toolInHand.GetComponent<SeedSack>();
             seedSack.seedingManager = seedingManager;
             seedSack.playerPosition = playerPosition;
             seedSack.posXLimit = posXLimit;
             seedSack.negXLimit = negXLimit;
             seedSack.posZLimit = posZLimit;
             seedSack.negZLimit = negZLimit;
         }
     }
 
     void SetTransformandName(GameObject tool, CurrentTool toolType)
     {
         string beforeTransform = string.Format("{0},{1},{2}", tool.transform.position,tool.transform.rotation,tool.transform.parent);
         Debug.Log(beforeTransform);
         tool.transform.parent = player.transform;
         string transformOfTool = string.Format("{0},{1},{2}", tool.transform.position, tool.transform.rotation, tool.transform.parent);
         Debug.Log(transformOfTool);
 
         switch (toolType)
         {
             case CurrentTool.Sense:
                 tool.name = string.Format("Sense");
                 break;
             case CurrentTool.Samen:
                 tool.name = string.Format("SamenSack");
                 break;
             case CurrentTool.Pflug:
                 tool.name = string.Format("Pflug");
                 break;
         }
     }
 }
 

This however turns up some problems.

First, the objects weren't actually interested in the positions I entered in the Save scripts. Then I realized that that was simply because I had animations running that kept them in place. Making the animations fit the positions then turned up the next problem:

The Sack of Seeds is instantiated in the correct place and then quickly flies away to about 500m away from the player, which is a problem. Here's the debug log:

(I had an output every frame, so here are the highlights):

Sack_of_Seeds (UnityEngine.GameObject),(1.9, 4.0, 2.0),(0.0, 0.0, 0.0, 1.0) UnityEngine.Debug:Log(Object)

(1.9, 4.0, 2.0),(0.0, 0.0, 0.0, 1.0), UnityEngine.Debug:Log(Object)

(1.9, 4.0, 2.0),(0.0, 0.0, 0.0, 1.0),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

Start: (1.9, 4.0, 2.0),(0.0, 0.0, 0.0, 1.0),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

Update: (1.9, 4.0, 2.0),(0.0, 0.0, 0.0, 1.0),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

Update: (3.8, 4.0, -0.1),(0.0, 0.0, 0.0, 1.0),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

Update: (21.2, 4.0, -18.3),(0.0, 0.0, 0.0, 1.0),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

Update: (149.5, 4.0, -114.1),(0.0, -0.2, 0.0, 1.0),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

Update: (728.2, 4.0, -87.3),(0.0, -0.7, 0.0, 0.8),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)

Update: (301.0, 4.0, -174.6),(0.0, -0.3, 0.0, 1.0),FPSController (UnityEngine.Transform) UnityEngine.Debug:Log(Object)


They are in chronological order.

As you can see, the object is in the correct place at first, but then dashes away and then comes back, but still keeps its distance. I checked the animations and they don't even work, even when I set the to loop.

There is one idle animation that the game doesn't even seem to care about and ignores until I click on it.

Any help whatsoever would be very much appreciated. I'll append some screenshots as well.

alt text

alt text

screenshot-2.jpeg (324.1 kB)
screenshot-1.jpeg (283.0 kB)
Comment
Add comment · Show 4
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 Light997 · Nov 28, 2015 at 04:02 PM 0
Share

$$anonymous$$y post is going through the moderation queue even though I have 56 karma. :(

@SaraCecilia

avatar image Owen-Reynolds Light997 · Nov 29, 2015 at 04:35 AM 0
Share

Well, you are posting a Help Room Q (multiple Qs, Debbuging Q) to the main area (I moved it to theHelpRoom.) So the accidental trip to the $$anonymous$$od Q works out in this case.

avatar image Light997 Owen-Reynolds · Nov 29, 2015 at 11:30 AM 0
Share

Thanks, I'll remember to do so.

avatar image Light997 · Nov 28, 2015 at 04:05 PM 0
Share

BTW, I just wanted to note that on German keyboards, Alt Gr + Q is the @ sign. In the tool used to make the texts, this triggers Blockquote, which means I have to go somewhere else and copy the @ sign.

0 Replies

· Add your reply
  • Sort: 

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

46 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

Related Questions

How to detect if a GameObject is currently rotating? 1 Answer

function behave unexpectedly inside a coroutine. 0 Answers

function behave unexpectedly inside a coroutine. 1 Answer

How do I animate child object to move into horizontal layout group. 0 Answers

I can not modify localscale while I'm playing because of the Animator. 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