Here are some free to use C Sharp snippets for your project:
How to make an object a 2D Object spin, Drawing UI's, 2D camera follow, 3D wandering AI,
Unity Dev Lab exist to show a handful or game samples which can be played from your browser! Feel free to play. 1) 2D Object spin 2) Tinkering with UI's
Below is a script on how to make a simple 2d object spin around in Unity using C Sharp. Feel free to use, just copy and paste onto your object. Remember, you will need to create a script in your project (2dspinner) and paste in the script below. I hope this helps.
// A Very Simple 2D Spin, paste me into your script and attache me to and object in your scene!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spin : MonoBehaviour {
// declare the speed varibale of
public float speed = 3.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate (0, speed, 0);
// placed in upadte this will turn based on the speed Var listed on line 8
}
}
// ————————
The script below draws up a very simple user interface. This is written in C Sharp, feel free to use in your project.
// How draw up a user UI using code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleImmiate : MonoBehaviour {
// Use this for initialization
// Attached to player draws a GUI buttom in the top left with test written inside it.
void OnGUI() {
if (GUI.Button (new Rect (10, 10, 40, 20), "Test")) {
Debug.Log ("Test button");
}
}
}
A nice camera follow script a bit like what is use in the UFO game. Notes on how to use attached the script below.
/// 2D Character CAMERA FOLLOW
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCamera : MonoBehaviour {
// attach this 2d camera follow script the camera
// Reference the player sprite to follow in the scene
// tweak the min and max properties unti the follow ratios are correct!
// MAKE SURE TO ATTACH TO CAMERA - NOT PLAYER
// distance in the x axis the player can move before
// camera follow
public float xMargin = 1.5f;
// distance in the y axis the player can move before the camera moves
public float yMargin = 1.5f;
// camera smoothness
public float xSmooth = 1.5f;
public float ySmooth = 1.5f;
//------------------
// Matrix4x4 cooridnate for cam
public Vector2 maxXAndY;
public Vector2 minXAndY;
public Transform player;
void Awake()
{
// reference player
player = GameObject.Find("playerSprite").transform;
if (player == null)
{
Debug.LogError ("player not found!");
}
}
//----
bool CheckXMargin()
{
// returns true distance
return Mathf.Abs
(transform.position.x - player.position.x) > xMargin;
}
bool CheckYMargin()
{
// returns true distance
return Mathf.Abs
(transform.position.y - player.position.y) > yMargin;
}
///
///
///
void FixedUpdate()
{
// by default the target x and y coorid
// are its currents x and y cooridaten
float targetX = transform.position.x;
float targetY = transform.position.y;
// player moved beyond margin
if (CheckXMargin ())
targetX = Mathf.Lerp (transform.position.x, player.position.x, xSmooth * Time.fixedDeltaTime);
// player moves Y.margin
if (CheckYMargin ())
targetY = Mathf.Lerp (transform.position.y, player.position.y, ySmooth *
Time.fixedDeltaTime);
// the traget x and y should not be bigger than max or smaller min
targetX = Mathf.Clamp (targetX, minXAndY.x, maxXAndY.x);
targetY = Mathf.Clamp (targetY, minXAndY.y, maxXAndY.y);
// set cam position target and z togthyer
transform.position = new Vector3 (targetX, targetY, transform.position.z);
}
}
/////// - —————— END
This script was attached to a wandering cube! The cube will bounce around from wall to wall.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WanderingAI : MonoBehaviour {
// float is variable to store ints / numbers health, speeds etc
// RAY CASTING variable
public float speed = 2.0f;
public float obstacleRange = 3.0f;
// Update is called once per frame (Continuous)
void Update () {
transform.Translate (0, 0, speed * Time.deltaTime);
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
if (Physics.SphereCast (ray, 0.75f, out hit)) {
if (hit.distance < obstacleRange) {
// float angle variable
float angle = Random.Range (-110, 260);
transform.Rotate (0, angle, 0);
}
}
// End if ---------------------
}
}
If you are a freelance coding heavy weight of developer looking for work feel to drop a line. If you are are looking for help you your latest game also feel free to drop get in touch
Hello!