Unity 3D 簡單 FPS 遊戲角本

上一篇主題 下一篇主題 向下

Unity 3D 簡單 FPS 遊戲角本

發表  aaa1218bbb 于 周五 11月 25, 2011 2:44 pm

NormalCharacterMotor.cs
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class NormalCharacterMotor : CharacterMotor {
private bool firstframe = true;

//Delete UpdateFacingDirection() function and maxRotationSpeed variable from here.

private void UpdateVelocity() {
CharacterController controller = GetComponent(typeof(CharacterController)) as CharacterController;
Vector3 velocity = controller.velocity;
if (firstframe) {
velocity = Vector3.zero;
firstframe = false;
}
if (grounded) velocity = Util.ProjectOntoPlane(velocity, transform.up);

// Calculate how fast we should be moving
Vector3 movement = velocity;
//bool hasJumped = false;
jumping = false;
if (grounded) {
// Apply a force that attempts to reach our target velocity
Vector3 velocityChange = (desiredVelocity - velocity);
if (velocityChange.magnitude > maxVelocityChange) {
velocityChange = velocityChange.normalized * maxVelocityChange;
}
movement += velocityChange;

// Jump
if (canJump && Input.GetButton("Jump")) {
movement += transform.up * Mathf.Sqrt(2 * jumpHeight * gravity);
//hasJumped = true;
jumping = true;
}
}

float maxVerticalVelocity = 1.0f;
AlignmentTracker at = GetComponent();
if (Mathf.Abs(at.velocitySmoothed.y) > maxVerticalVelocity) {
movement *= Mathf.Max(0.0f, Mathf.Abs(maxVerticalVelocity / at.velocitySmoothed.y));
}

// Apply downwards gravity
movement += transform.up * -gravity * Time.deltaTime;

if (jumping) {
movement -= transform.up * -gravity * Time.deltaTime / 2;

}

// Apply movement
CollisionFlags flags = controller.Move(movement * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

// Update is called once per frame
void Update () {
if (Time.deltaTime == 0 || Time.timeScale == 0)
return;

//Delete call to the UpdateFacingDirection() function.

UpdateVelocity();
}
}


//===========================================
RagdollInstantiator.cs
using UnityEngine;
using System.Collections;
public class RagdollInstantiator : MonoBehaviour {

public GameObject deadReplacement;

//remove cameraTargetPath variable from here. Add new variable called DeathCamera as a GameObject;

public GameObject DeathCamera;

void Die () {
//Enable death camera when character die.
DeathCamera.active = true;
//Set the death camera position to character position plus above his head.
DeathCamera.transform.position = transform.position + new Vector3 (0,5,0);
// Replace ourselves with the dead body
GameObject dead = null;
if (deadReplacement) {
// Create the dead body
dead = (GameObject)Instantiate (deadReplacement, transform.position, transform.rotation);

Vector3 vel = Vector3.zero;
if (rigidbody) {
vel = rigidbody.velocity;
}
else {
CharacterController cc = GetComponent ();
vel = cc.velocity;
}

// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse (transform, dead.transform, vel);

gameObject.SetActiveRecursively(false);
}
}

void CopyTransformsRecurse (Transform src, Transform dst, Vector3 velocity) {

Rigidbody body = dst.rigidbody;
if (body != null) {
body.velocity = velocity;
body.useGravity = true;
}

dst.position = src.position;
dst.rotation = src.rotation;

foreach (Transform child in dst) {
// Match the transform with the same name
Transform curSrc = src.Find (child.name);
if (curSrc)
CopyTransformsRecurse (curSrc, child, velocity);
}
}
}

//===========================================
ShooterGameCamera.cs

using UnityEngine;
using System.Collections;
// 3rd person game-like camera controller
// keeps camera behind the player and aimed at aiming point
public class ShooterGameCamera : MonoBehaviour {

public Transform player;
public Transform aimTarget;

public Texture reticle;
private Transform cam;
private LayerMask mask;

// Use this for initialization
void Start ()
{
// Add player's own layer to mask
mask = 1 << player.gameObject.layer;
// Add Igbore Raycast layer to mask
mask |= 1 << LayerMask.NameToLayer("Ignore Raycast");
// Invert mask
mask = ~mask;

cam = transform;
}

// Update is called once per frame
void LateUpdate () {
// Before changing camera, store the prev aiming distance.
// If we're aiming at nothing (the sky), we'll keep this distance.
float prevDist = (aimTarget.position - cam.position).magnitude;

RaycastHit hit;
// Do a raycast from the camera to find the distance to the point we're aiming at.
float aimTargetDist;
if (Physics.Raycast(cam.position, cam.forward, out hit, 100, mask)) {
aimTargetDist = hit.distance + 0.05f;
}
else {
// If we're aiming at nothing, keep prev dist but make it at least 5.
aimTargetDist = Mathf.Max(5, prevDist);
}

// Set the aimTarget position according to the distance we found.
// Make the movement slightly smooth.
aimTarget.position = cam.position + cam.forward * aimTargetDist;
}

void OnGUI () {
if (Time.time != 0 && Time.timeScale != 0)
GUI.DrawTexture(new Rect(Screen.width/2-(reticle.width*0.5f), Screen.height/2-(reticle.height*0.5f), reticle.width, reticle.height), reticle);
}
}

//=====================================
MouseLook.cs

using UnityEngine;
using System.Collections;
/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
/// -> A CharacterMotor and a CharacterController component will be automatically added.
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
//Change MouseLook script's Update() function to LateUpdate() so the movements of the camera remains fluid.
void LateUpdate ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}

void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}

_________________
歡迎大家多多交流~


aaa1218bbb
社員
社員

文章數 : 65
積分 : 2084
注冊日期 : 2011-11-22

檢視會員個人資料 http://design.gugebb.com

回頂端 向下

上一篇主題 下一篇主題 回頂端


 
這個論壇的權限:
無法 在這個版面回復文章