Hello,
I made a little JavaScript for Planting and gathering,
Now this is my script;
Onpickup.js
#pragma strict
public var cube : Transform;
var seedsCount : UI.Text;
var plantCount : UI.Text;
private var playerPos:Vector3;
private var spawnPos:Vector3;
public var player: GameObject;
var SAmount = 5;
var Plant = 0;
//Gather
var gatherDistance = 2;
var gatherTime = 1;
//Boolean
var inRange = false;
function Start () {
}
function Update ()
{
seedsCount.text = "Seeds: " + SAmount;
plantCount.text = "Planted: " + Plant;
//Gather
var player : Vector3 = gameObject.Find("sux(clone)").transform.position;
var moveDirection : Vector3 = player - transform.position;
if(moveDirection.magnitude < gatherDistance)
{
inRange = true;
}
else if(moveDirection.magnitude > gatherDistance)
{
inRange = false;
}
if(inRange == true)
{
if(Input.GetKeyDown(KeyCode.J))
{
print("J is pressed");
DoGather();
}
}
if(Input.GetKeyDown(KeyCode.K))
{
print("K is pressed");
DoPlant();
}
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.name == "Cube")
{
print("Cube");
Destroy(col.gameObject);
}
}
function DoPlant()
{
if(SAmount == 0)
{
print("Not enough seeds!");
}
else if(SAmount > 0)
{
playerPos = player.transform.position;
spawnPos = Vector3(playerPos.x, playerPos.y-0.5, playerPos.z); ///change as needed
Instantiate (cube, spawnPos, Quaternion.identity);
SAmount -= 1;
Plant += 1;
}
}
function DoGather()
{
yield WaitForSeconds(gatherTime);
Destroy(gameObject);
Plant -= 1;
}
It is attached to my Character,
The planting works, but the problem is the gathering.
It shows me this error;
^ Thats another script that used to work BEFORE i modified the onpickup.js script
So basically what the Gather should do; Check if Character is near by the plant, if in between 2, then inRange goes too true and then i can press J too gather it.