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.
$$anonymous$$y post is going through the moderation queue even though I have 56 karma. :(
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.
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.
Your answer
Follow this Question
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