I am trying to get my Player to “collect” wood when he collides with a tree. Both the Player and the
trees are Prefab Rigidbodies with BoxColliders. Physics detects the collision and sends the player
backwards, but the OnCollisionEnter() methed never gets called.
Here is the code for the TreeManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TreeManager : MonoBehaviour
{
public GameObject TreePrefab;
public int numberTrees = 18;
private PlayerController playerControllerScript;
private Vector3 treeLocations;
private int xpos, zpos;
// Use this for initialization
void Start()
{
playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
treeLocations = new Vector3[numberTrees];
for (int i = 0; i < numberTrees; i++)
{
TreePrefab.transform.localScale = new Vector3(0.38f, 0.52f, 0.38f);
xpos = 180 + Random.Range(0, 20);
zpos = 83 + Random.Range(0, 12);
treeLocations *= new Vector3(xpos, 0, zpos);*
-
Instantiate(TreePrefab, new Vector3(xpos, 0, zpos), TreePrefab.transform.rotation);*
print(TreePrefab.name + " at (" + treeLocations.x + ", " + treeLocations_.y + ", " + treeLocations*.z + “)”);
}
}
// Update is called once per frame*
* void Update()
{
}
public void OnCollisionEnter()
{
playerControllerScript.woodTotal += 200;
print(gameObject.name + playerControllerScript.woodTotal);
Destroy(gameObject);
}
}
and the code for the PlayerController is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 75;
public float woodTotal;
Rigidbody playerRb;
// Use this for initialization*
* void Start () {
playerRb = gameObject.GetComponent();
}*_
* // Update is called once per frame*
* void LateUpdate () {*
* if (Input.GetKeyDown(KeyCode.UpArrow))*
{
_ playerRb.AddForce(Vector3.forward * speed * Time.deltaTime, ForceMode.Impulse);
}
* if (Input.GetKeyDown(KeyCode.LeftArrow))
{
playerRb.AddForce(Vector3.left * speed * Time.deltaTime, ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
playerRb.AddForce(Vector3.back * speed * Time.deltaTime, ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
playerRb.AddForce(Vector3.right * speed * Time.deltaTime, ForceMode.Impulse);
}
}
/
* void OnCollision(Collision collision)
{
if (collision.gameObject.CompareTag(“Tree”))
{*_
* woodTotal += 200;*
* Destroy(collision.gameObject);*
* }*
* }*
_ /
}
Any help would be greatly appreciated!,I am still new to Unity. My game needs the player to collide with a tree, pause a few seconds, then remove the tree from the scene and credit the player with 200 wood. Both the player and instantiated trees are Prefab Rigidbody with BoxCollider. The player simple bounces off the tree, so the Physics is working apparently. But the code inside the OnCollision() never gets executed.
public class TreeManager : MonoBehaviour
{
public GameObject TreePrefab;
public int numberTrees = 2;
private PlayerController playerControllerScript;
private Vector3 treeLocations;
private int xpos, zpos;
I placed this in the TreeManager script that spawns the trees
public class TreeManager : MonoBehaviour*
{
* public GameObject TreePrefab;
public int numberTrees = 18;
private PlayerController playerControllerScript;
private Vector3 treeLocations;
private int xpos, zpos;
void Start()
{
playerControllerScript = GameObject.Find(“Player”).GetComponent();
treeLocations = new Vector3[numberTrees];
for (int i = 0; i < numberTrees; i++)
{
TreePrefab.transform.localScale = new Vector3(0.38f, 0.52f, 0.38f);
xpos = 180 + Random.Range(0, 20);
zpos = 83 + Random.Range(0, 12);
treeLocations = new Vector3(xpos, 0, zpos);
Instantiate(TreePrefab, new Vector3(xpos, 0, zpos), TreePrefab.transform.rotation);
print(TreePrefab.name + " at (" + treeLocations.x + ", " + treeLocations.y + ", " + treeLocations.z + “)”);
}
}
public void OnCollisionEnter()
{
playerControllerScript.woodTotal += 200;
print(gameObject.name + playerControllerScript.woodTotal);
Destroy(gameObject);
}*
The PlayerManager script simply moves the player around the screen with .AddForce.
Any help would be appreciated! Thanks!_