using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AppleTree : MonoBehaviour
{
// Prefab for instantiating apples
public GameObject applePrefab;
public float Speed;
public float maxSpeed;
// Speed at which the AppleTree moves in meters/second
public float speed = 2f;
// Distance where AppleTree turns around
public float leftAndRightEdge = 15f;
// Chance that the AppleTree will change directions
public float chanceToChangeDirections = 1f;
// Rate at which Apples will be instantiated
public float secondsBetweenAppleDrops = 1f;
void Start()
{
// Dropping apples every second
InvokeRepeating("DropApple", 2f, secondsBetweenAppleDrops);
}
void DropApple()
{
GameObject Apple = Instantiate(applePrefab) as GameObject;
Apple.transform.position = transform.position;
}
void Update()
{
// Basic Movement
Vector3 pos = transform.position;
pos.x += speed * Time.deltaTime;
transform.position = pos;
// Changing Direction
if (pos.x < -leftAndRightEdge)
{
speed = Mathf.Abs(speed); // Move right
}
else if (pos.x > leftAndRightEdge)
{
speed = -Mathf.Abs(speed); // Move left
}
if(Speed < maxSpeed)
Speed += 0.5f * Time.deltaTime;
{
if (Input.GetKey("escape"))
{
Application.Quit();
}
}
}
void FixedUpdate()
{
// Changing Direction Randomly
if (Random.value < chanceToChangeDirections)
{
speed *= -1; // Change direction
}
}
}
Hello there, so I’m trying out an apple picker game prototype from a introduction book (I’m a beginner) and I want to find a way to simply add a toggle in the code that changes or adds variables to the speed .
