EDIT - Answer helped - created knew unknown problem.
Skip slightly past First answer, under the 3rd body of codes I reply with a new issue in regards to a myTransform conflicting with Ref’ to another object (neither are interacting in the script as far as I know - it throws a null error) any help would be great guys.
Ok so I’ve got something else I want to learn. I’ll try my best to explain the situation because I’m getting a bit deeper and deeper… my goal is still to chop down a tree using click moving pathfinding.
What I’ve got so far
- Hover over, change color, on exit change back.
- If clicked check player dist, if out of range, leave cutDown false
- If within range, set cutDown to true
- GOAL - send a message, or access cutDown in ChopTree to Player to determine if true so I can 1) Disable Movement, 2) Face Tree - I think I’ll be fine in regards to LookAt calls once I do get to determine if I am really ready to cut down a tree.
These scripts are in a slightly mangled state at this time since I’ve been trying so many methods. Not looking for someone to write the script for me, looking for a better job at reading variables from another script. I looked over Sendmessage and Component in the ref - I understand Component and like I said how to change values - I only want to read… I’ve tried both methods but I can’t seem to get the syntax right or find what I need as most examples involve a FP view, or 3rd with direct character control and not pathfinding control… Sorry for the long wall of text but I wanted to explain in detail rather then not.
CharacterAnimation script
enter code hereusing System.Collections;
public class CharacterAnimations : MonoBehaviour {
//THE SCRIPT THAT IS IN THE SAME OBJECT IS CALLED "AIFollow" I do a public attach here.
//Attach to AIFollow to get speed
public AIFollow script;
//Bool for is movving happenin to enable run animation? If stop'd no run.
public bool isMove;
//Keeps track of movement
Transform myTransform;
//Stores the transform at last vector
Vector3 lastPos;
void Start()
{
//ref the script AIFollow at the start
script = GetComponent<AIFollow>();
//declare move at false at start
isMove = false;
//establish a way to track this object
myTransform = transform;
//establish a way to store last vector from object
lastPos = myTransform.position;
}
void Update(){
if ( myTransform.position != lastPos )
isMove = true;
else
isMove = false;
//After checking to see if update was the same as old position, MAKE them the same.
lastPos = myTransform.position;
//If old vector was the same, move will be false and this will be bypassed.
if (Input.GetKey(KeyCode.LeftShift) && isMove != false){
script.speed = 14;
animation.CrossFade ("mRunning");
}else{
script.speed = 5;
}
//if its bypassed and shift is not down, walk
if(script.speed == 5 && isMove != false){
animation.CrossFade("mWalking");
}
//since it was bypassed, and ismove is false, he cannot be moving. Display idle animation.
if (isMove == false){
animation.CrossFade("mIdle");
}
}
//Function to make character get msg and turn toward tree sending it.
void LookAtTree(bool cutDown)
{
Debug.Log("CutDown MSG IN");
}
}
ChopTree script
enter code hereusing UnityEngine;
using System.Collections;
public class ChopTree : MonoBehaviour {
//set tree health
//private int treeHealth = 30;
//Cut tree down
public bool cutDown = false;
//ref Player
public GameObject Player;
//distance to tree?
public float playerDist;
//store orginal color
private Color startcolor;
//display GUI text
private bool showGUI = false;
void Start()
{
//ref
Player = GameObject.Find ("Reiss");
}
void Update()
{
}
//Mouse over event to store color of tree, change red, and then show chop tree
void OnMouseEnter(){
startcolor = renderer.material.color;
renderer.material.color = Color.red;
showGUI = true;
}
//Mouse over event to remove GUI display and return to normal color
void OnMouseExit()
{
renderer.material.color = startcolor;
showGUI = false;
}
//information about the GUI to display and where to display it.
void OnGUI()
{
if (showGUI)
{
//displace text in center screen
GUI.Label (new Rect(Screen.width/2-50,Screen.height/2-50,100,20),"Chop Tree?");
//check if mouse is down and within distance
if(Input.GetMouseButtonDown(0))
{
float playerDist = Vector3.Distance(Player.transform.position, this.transform.position);
CheckDistance();
//Debug.Log(playerDist);
if(playerDist < 13)
{
//if true going to start animations and damage to tree
cutDown = true;
SendMessage("LookAtTree",Player);
Debug.Log(cutDown);
}
}
if (cutDown)
{
}
}
}
//Attempting to use a function call to check distance so update doesn't do it all the time.
public float CheckDistance()
{
float playerDist = Vector3.Distance(Player.transform.position, this.transform.position);
return playerDist;
}
}