I’m making a little game where a beaver chews down trees and gathers logs to use for building a dam. The trees get destroyed once they’re chewed down. Then, when the player wants to use them for building, they left-click to instantiate the trunk (the log) in front of the beaver, right-click to rotate it how they want (one of three orientations), and then left-click again to place it at some location. The problem I’m having is instantiating the trunk so that it stays in front of the beaver, initially (until the player right-clicks) oriented left-to-right, as it moves and jumps. The trunk also isn’t meant to detect collisions during this time, though I don’t think I’m having a problem with that.
I’ve gotten it to where the trunk instantiates in the correct position and at the correct initial rotation, but it wouldn’t follow the beaver upward when the beaver jumped. However, I’ve spent several hours trying to fix the problem, messing one thing up while fixing another, and I don’t remember how I got as far as I did then. My current problem seems to be that the trunk is stuck instantiating at a set world position and rotation instead of ones local to the beaver. I’m sure this is an easy issue to fix, but every time I go into the code, I can’t fix it.
Current code below. I’m trying to write the ReadyLog() function:
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
// Current number of logs possessed
int logNum = 0;
public GUISkin logSkin;
public Rect displayArea;
public Rect logDisplay;
// Indicates if a log has been instantiated (with the ReadyLog() function)
bool logReadied = false;
// Reference to the log in use
GameObject log;
// Reference to the tree prefab
public GameObject tree;
// For the orientation of the instantiated, but not-yet-placed, log
Direction dir = Direction.LeftRight;
void OnGUI(){
GUI.skin = logSkin;
GUI.BeginGroup(displayArea);
GUI.Label(logDisplay, logNum.ToString());
GUI.EndGroup();
}
// For the orientation of the instantiated, but not-yet-placed, log
enum Direction{
LeftRight,
UpDown,
ForwardBackward
};
// Called from another script, increments the number of logs possessed
void AddLog(){
logNum++;
}
// Instantiates the tree model's trunk and locks it in place relative to the beaver object
void ReadyLog(){
log = Instantiate(tree, /*PROBLEM CODE GOES HERE*/) as GameObject;
log.transform.parent = transform;
/*PROBLEM CODE MAY ALSO GO HERE*/
log.rigidbody.detectCollisions = false;
log.transform.FindChild("Leaf").renderer.enabled = false;
logReadied = true; //Indicates readiness for log's placement in world
}
// Locks the log in place after it's been instantiated and rotated (if desired)
void PlaceLog(){
log.transform.parent = null;
log.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
logNum--;
}
// Rotates the log to one of three orientations, in sequence
void TurnLog(){
if(dir==Direction.LeftRight){
log.transform.RotateAround(Vector3.forward,90f);
dir = Direction.UpDown;
}
if(dir==Direction.UpDown){
log.transform.RotateAround(Vector3.right,90f);
dir = Direction.ForwardBackward;
}
if(dir==Direction.ForwardBackward){
log.transform.RotateAround(Vector3.up,90f);
dir = Direction.LeftRight;
}
}
// Detects when the player wants to instantiate, and then place, a log
void Update(){
if(!logReadied logNum > 0 Input.GetMouseButtonDown(0)){
ReadyLog();
}
if(logReadied Input.GetMouseButtonDown(0)){
PlaceLog();
}
if(logReadied Input.GetMouseButtonDown(1)){
TurnLog();
}
}
}
I’m also aware that my GUI doesn’t currently work, but that’s something I’ll look into after this issue is resolved. I have Will Goldstone’s great book that I used to construct a tutorial game, so I have some prior experience with things.
Thanks in advance for any assistance.