0% found this document useful (0 votes)
43 views

Codes

The document contains code for several scripts that control different aspects of a Unity game: 1. A cameraFollow script that makes the camera follow and smoothly track a target transform. 2. A fireLazer script that fires a projectile from a weapon when the fire button is pressed and handles facing direction. 3. A ShootBullet script that shoots a raycast from a weapon and damages targets if it hits. 4. A ghostcontroller script that controls player movement, jumping, animation, and flipping direction. 5. A DestroyMe script that destroys a game object after a set amount of time.

Uploaded by

api-301384761
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Codes

The document contains code for several scripts that control different aspects of a Unity game: 1. A cameraFollow script that makes the camera follow and smoothly track a target transform. 2. A fireLazer script that fires a projectile from a weapon when the fire button is pressed and handles facing direction. 3. A ShootBullet script that shoots a raycast from a weapon and damages targets if it hits. 4. A ghostcontroller script that controls player movement, jumping, animation, and flipping direction. 5. A DestroyMe script that destroys a game object after a set amount of time.

Uploaded by

api-301384761
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

cameraFollow:

using UnityEngine;
using System.Collections;
public class cameraFollow : MonoBehaviour {
public Transform target;
public float smoothing = 5f;
Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - target.position;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp (transform.position, targetCamPos,
smoothing * Time.deltaTime);
}
}
fireLazer:
using UnityEngine;
using System.Collections;
public class fireLazer : MonoBehaviour {
public float timeBetweenBullets = 0.15f;
public GameObject projectile;
float nextBullet;

// Use this for initialization


void Awake () {
nextBullet = 0f;

// Update is called once per frame


void Update () {
ghostcontroller myPlayer =
transform.root.GetComponent<ghostcontroller> ();
if (Input.GetAxisRaw ("Fire1") > 0 && nextBullet < Time.time) {
nextBullet = Time.time + timeBetweenBullets;
Vector3 rot;
if(myPlayer.GetFacing() == -1f) {
rot = new Vector3 (0, -90, 0);
}
else rot = new Vector3 (0,90,0);
Instantiate(projectile, transform.position, Quaternion.Euler(rot));
}
}
}

ShootBullet:
using UnityEngine;
using System.Collections;
public class ShootBullet : MonoBehaviour {
public float range = 10f;
public float damage = 5f;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
LineRenderer gunLine;
// Use this for initialization
void Awake () {
shootableMask = LayerMask.GetMask ("Shootable");
gunLine = GetComponent<LineRenderer>();
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
gunLine.SetPosition(0,transform.position);
if (Physics.Raycast (shootRay, out shootHit, range, shootableMask)) {

//hit an enemy goes here


gunLine.SetPosition (1, shootHit.point);
}
else gunLine.SetPosition(1, shootRay.origin+shootRay.direction*range);
}
// Update is called once per frame
void Update () {
}
}

ghostcontroller:
using UnityEngine;
using System.Collections;
public class ghostcontroller : MonoBehaviour {
// movement
public float moveSpeed;
public float walkSpeed;
Rigidbody myRB;
Animator myAnim;
bool facingRight;
//for jumping
bool grounded = false;
Collider[] groundCollisions;
float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
void Start () {
myRB = GetComponent<Rigidbody>();
myAnim = GetComponent<Animator>();
facingRight = true;

}
// Update is called once per frame
void Update () {
}
void FixedUpdate () {
if (grounded && Input.GetAxis ("Jump") > 0) {
grounded = false;
myAnim.SetBool ("grounded", grounded);
myRB.AddForce(new Vector3 (0, jumpHeight, 0));
}
groundCollisions = Physics.OverlapSphere (groundCheck.position,
groundCheckRadius, groundLayer);
if (groundCollisions.Length > 0)
grounded = true;
else
grounded = false;
myAnim.SetBool ("grounded", grounded);
float move = Input.GetAxis ("Horizontal");
myAnim.SetFloat ("Speed", Mathf.Abs (move));
float sneaking = Input.GetAxisRaw ("Fire3");
myAnim.SetFloat ("sneaking", sneaking);
float firing = Input.GetAxis ("Fire1");
myAnim.SetFloat ("shooting", firing);
if ((sneaking > 0 ||firing>0) && grounded) {
myRB.velocity = new Vector3 (move * walkSpeed,
myRB.velocity.y, 0);
}
else {
myRB.velocity = new Vector3 (move * moveSpeed, myRB.velocity.y, 0);
}
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}

void Flip (){


facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.z *= -1;
transform.localScale = theScale;
}
public float GetFacing () {
if (facingRight)
return 1;
else
return -1;
}
}

DestroyMe:
using UnityEngine;
using System.Collections;
public class DestroyMe : MonoBehaviour {
public float aliveTime;

// Use this for initialization


void Awake () {
Destroy (gameObject, aliveTime);

}
// Update is called once per frame
void Update () {
}
}

You might also like