Hi,
I’m trying to make a small game where a pig ‘eats’ coins. I have a prefab with an empty parent (myCoinParent for location), a coin (coin), and another empty (myCoinTarget) that the pig uses to line itself up to the right location. I’m trying to get it so that the trigger will send the object information to the pig’s script, which will then be used to zero in on the proper location, trigger the respective animators, and then delete the coin when the animation finishes. As it stands the scripts I have work fine for a single coin, but the script does nothing (or bugs out) after the first one.
Having made the gameobjects public on the pig (tankcontroller.cs), I can see that the GameObjects are already populated before the trigger. They definitely shouldn’t be. I’m really at a loss for what I’m doing wrong, so any help is appreciated.
(apologies in advance for newbie spaghetti code)
public class Collectables : MonoBehaviour {
public int totalCollected;
public float rotateSpeed = 20000f;
public TankController myTankController;
void Start () {
}
// Update is called once per frame
void Update () {
gameObject.transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
}
private void OnTriggerEnter(Collider other)
{
SendStuff(gameObject);
}
public void SendStuff(GameObject coin)
{
coin = gameObject;
myTankController.coin = coin;
GameObject myCoinParent = gameObject.transform.parent.gameObject;
myTankController.myCoinParent = myCoinParent;
GameObject myCoinTarget = gameObject.transform.Find("myCoinTarget").gameObject;
myTankController.myCoinTarget = myCoinTarget;
}
}
here’s the (much longer and poorly coded) character/animator controller I’m working on:
Vector3 targetPosition;
Vector3 lookAtTarget;
Vector3 velocityVector;
public Vector3 transformPosition;
public Vector3 myCoinTarget_Vector;
public GameObject myCoinTarget;
public GameObject coin;
public GameObject myCoinParent;
public int totalCollected;
Quaternion playerRot;
float rotSpeed = 3f;
float speed = 0f;
bool moving = false;
bool movingConnectEmpties = false;
public bool connectedEmpties = false;
bool shootingCoins = false;
public float offsetMagnitude = 0;
float runDistance = 5f;
public Animator myCoinAnimator;
public Animator myPigAnimator;
public string animClipName;
float animCurrentClipLength;
AnimatorClipInfo[] animCurrentClipInfo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if(coin != null)
{
myCoinTarget_Vector = myCoinTarget.transform.position;
}
myPigAnimator.SetFloat("offsetMagnitude", offsetMagnitude);
transformPosition = transform.position;
AnimatorInfo();
if (Input.GetMouseButton(0))
{
myPigAnimator.SetTrigger("walk");
SetTargetPosition();
}
else if (moving == true)
{
Move();
}
else if (moving == false && movingConnectEmpties == true && connectedEmpties == false)
{
SetCoinPosition();
ConnectEmpties();
}
else if (totalCollected >= 5)
{
moving = false;
movingConnectEmpties = false;
connectedEmpties = false;
myPigAnimator.SetTrigger("explode");
}
}
void SetTargetPosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
targetPosition = hit.point;
//this.transform.LookAt(targetPosition);
lookAtTarget = new Vector3(targetPosition.x - transform.position.x,
transform.position.y,
targetPosition.z - transform.position.z);
playerRot = Quaternion.LookRotation(lookAtTarget);
myPigAnimator.SetTrigger("walk");
moving = true;
offsetMagnitude = new Vector3(targetPosition.x - transform.position.x, targetPosition.x - transform.position.y, targetPosition.z - transform.position.z).magnitude;
}
}
void Move()
{
if(offsetMagnitude >= 10f)
{
speed = 4f;
print("Speed: " + speed);
}
else if(offsetMagnitude < 10f)
{
speed = 2f;
print("Speed: " + speed);
}
transform.rotation = Quaternion.Slerp(transform.rotation, playerRot, rotSpeed * Time.deltaTime);
velocityVector = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
transform.position = velocityVector;
if(transformPosition == targetPosition)
{
moving = false;
offsetMagnitude = 0;
}
}
public void AnimatorInfo()
{
//Fetch the current Animation clip information for the base layer
animCurrentClipInfo = this.myPigAnimator.GetCurrentAnimatorClipInfo(0);
//Access the current length of the clip;
animCurrentClipLength = animCurrentClipInfo[0].clip.length;
//Access the Animation clip name
animClipName = animCurrentClipInfo[0].clip.name;
}
void OnTriggerEnter(Collider col)
{
//col.gameObject.GetComponent<Collectables>().SendStuff(coin);
movingConnectEmpties = true;
moving = false;
}
void SetCoinPosition()
{
lookAtTarget = new Vector3(myCoinTarget_Vector.x - transform.position.x,
transform.position.y,
myCoinTarget_Vector.z - transform.position.z);
playerRot = Quaternion.LookRotation(lookAtTarget);
myPigAnimator.SetTrigger("walk");
offsetMagnitude = new Vector3(myCoinTarget_Vector.x - transform.position.x, myCoinTarget_Vector.x - transform.position.y, myCoinTarget_Vector.z - transform.position.z).magnitude;
print("Offset magnitude is :" + offsetMagnitude);
}
void ConnectEmpties()
{
if (offsetMagnitude >= 10f)
{
speed = 4f;
print("Speed: " + speed);
}
else if (offsetMagnitude < 10f)
{
speed = 2f;
print("Speed: " + speed);
}
transform.rotation = Quaternion.Slerp(transform.rotation, playerRot, rotSpeed * Time.deltaTime);
velocityVector = Vector3.MoveTowards(transform.position, myCoinTarget_Vector, speed * Time.deltaTime);
transform.position = velocityVector;
if (transform.position == myCoinTarget_Vector)
{
myCoinAnimator = coin.GetComponent<Animator>();
moving = false;
movingConnectEmpties = false;
connectedEmpties = true;
speed = 0;
//myPigAnimator.SetTrigger("idle");
myPigAnimator.SetTrigger("snort");
myCoinAnimator.SetBool("suck", true);
Destroy(myCoinParent, 1.3f);
connectedEmpties = false;
}
}
}
any help would be appreciated!