Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scripts for the simple HP and Mana Potion exmple
using UnityEngine;
using System.Collections;
// Destroys the game object it is attached to after the given timeframe
public class DsetroyAfterTime : MonoBehaviour
{
public float DestoryAfterTime = 1.0f;
void Start ()
{
Destroy(gameObject, DestoryAfterTime);
}
}
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
// The CharacterController component from inside unity.
// Make sure to add this by making your game object then
// going to Component->Physics->Character Controller
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update ()
{
ProcessInput();
}
void ProcessInput()
{
// Check out http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
// for more information on how this works. Basically it reads the user input and then moves
// the character controller appropriately
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class PotionBehavior : MonoBehaviour
{
// The list of tags that this effect will interact with when triggered
public List<String> TagsToInteractWith;
// The pickup effect to play when this game object is triggered
public GameObject PickupEffect;
// If the effect created is attached to the colliding object
public bool AttachToCollidedObject = true;
// How long to wait before destroying this object when its not attached
public float DestroyTimeAfterTrigger = 1.5f;
void OnTriggerEnter(Collider other)
{
if (TagsToInteractWith.Contains(other.gameObject.tag))
{
// The effect we create when the pickup is triggered
GameObject pickupEffect = (GameObject)Instantiate(PickupEffect, AttachToCollidedObject ? other.gameObject.transform.position : gameObject.transform.position, Quaternion.identity);
if (AttachToCollidedObject)
{
pickupEffect.transform.parent = other.gameObject.transform;
Destroy(gameObject);
return;
}
Destroy(gameObject, DestroyTimeAfterTrigger);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.