i wrote 3 scripts that work together to make a simple platform game
main ideas
use can do some simple some parkour(grab on ledges,climb, run on walls, and slide)
you will have to get 10 item in a set time and avoid to get caught
Problems :
i am stuck with climbing please help
INPUT MANAGER
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour {
// INPUT MANAGER
public static CharacterController characterController;
public static TP_Controller Instance; // refrence to this scrpit
public float RunMultiplier = 2f;
public bool HangEnable;
public Transform ClimbPoint{get;set;} // the transform in which the character will hang
// Use this for initialization
void Awake () {
characterController = GetComponent("CharacterController") as CharacterController;
Instance = this;
}
// Update is called once per frame
void Update () {
if(Camera.mainCamera == null)
return;
GetLocomotionInput();
HandleActionInput();
TP_Motor.Instance.UpdateMotor();
// check to see if we are standing near a cliff
RaycastHit hit;
if(Physics.Raycast(GameObject.Find("Raycaster").transform.position,transform.up,out hit,10f))
{
if(hit.transform.tag == "Ledges")
{
hit.transform.gameObject.SendMessage("OnRayCastHit");
//Debug.Log("Ledge Found");
}
}
else
HangEnable = false;
}
void GetLocomotionInput() // input axis manager
{
var deadZone = 0.1f;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
if(Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < deadZone)
TP_Motor.Instance.MoveVector += new Vector3 (0,0,Input.GetAxis("Vertical"));
if(Input.GetAxis("Vertical") >= 1f)
{
if(Input.GetButton("Run"))
{
TP_Motor.MoveSpeed *= RunMultiplier ;
}
if(TP_Motor.MoveSpeed >= 20f)
TP_Motor.MoveSpeed = 20f;
}
if(Input.GetAxis("Vertical") >= 1f)
{
if(!Input.GetButton("Run"))
TP_Motor.MoveSpeed = 10f;
}
if(Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < deadZone)
TP_Motor.Instance.MoveVector += new Vector3 (Input.GetAxis("Horizontal"),0,0);
if(ClimbPoint)
Debug.Log("I found it");
}
void HandleActionInput () // input action manager
{
if(Input.GetButton("Jump"))
{
if(HangEnable)
Grab();
else
Jump();
}
}
public void Jump() // call the Jump() method in TP_Motor
{
TP_Motor.Instance.Jump();
}
public void Grab() // call the Grab() method in TP_Motor
{
TP_Motor.Instance.Grab();
}
public void SetClimbPoint(Transform climbPoint) // get the transform for ClimbPoint
{
climbPoint = ClimbPoint;
HangEnable = true;
}
public void ClearClimbPoint(Transform climbPoint)// clear the transform
{
climbPoint = null;
}
}
MOTION MANAGER
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour {
public static TP_Motor Instance; // refrence to this script
public static float MoveSpeed = 10f; // the move speed
public Vector3 MoveVector{get; set;} // the move Vector3
public float Gravity = 21f; // acceleration to the grond
public float TerminalVelocity = 20f; // the maxspeed of gravity
public float VerticalVelocity {get;set;} // the saved y vector to reapply
public float JumpSpeed;
// Use this for initialization
void Awake ()
{
Instance = this;
}
public void UpdateMotor () // will be call be TP_Controller
{
SnapOnCharacterWithCamera();
ProcessMotion();
}
void ProcessMotion() // process the motion of the player input
{
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection(MoveVector);
//Normalize MoveVector if Magnitude > 1
if(MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
//Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed;
// Reapplying gravity
MoveVector = new Vector3 (MoveVector.x,VerticalVelocity,MoveVector.z);
//Applying gravity
ApplyGravity();
// Move the character in world space
TP_Controller.characterController.Move(MoveVector * Time.deltaTime);
}
void SnapOnCharacterWithCamera()
{
if(MoveVector.x !=0 || MoveVector.z != 0)
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,Camera.mainCamera.transform.eulerAngles.y,transform.rotation.eulerAngles.z);
}
}
void ApplyGravity()
{
if(MoveVector.y > -TerminalVelocity)
MoveVector = new Vector3 (MoveVector.x,MoveVector.y - Gravity * Time.deltaTime,MoveVector.z);
if(TP_Controller.characterController.isGrounded && MoveVector.y < -1)
MoveVector = new Vector3 (MoveVector.x,-1,MoveVector.z);
}
public void Jump()
{
if(TP_Controller.characterController.isGrounded)
VerticalVelocity = JumpSpeed;
}
public void Grab()
{
transform.position = Vector3.Lerp(transform.position,new Vector3 (TP_Controller.Instance.ClimbPoint.position.x,TP_Controller.Instance.ClimbPoint.position.y,TP_Controller.Instance.ClimbPoint.position.z),Time.time);
Gravity = 0;
}
}
LEDGE SCRIPT
using UnityEngine;
using System.Collections;
public class Ledge : MonoBehaviour {
public static Ledge Instance; // refrence to this scipt
void Awake()
{
Instance = this;
}
public void OnRayCastHit() // when the raycast hit will this metod
{
TP_Controller.Instance.SetClimbPoint(transform);
//Debug.Log("Here i am");
}
//public void OnRayCastNotHit()
//{
// TP_Controller.Instance.ClearClimbPoint(transform);
//}
void OnTriggerEnter(Collider hit) // These are test codes
{
if(hit.transform.tag == "Player")
Debug.Log("Enter Climbable edge");
}
void OnTriggerExit(Collider hit)
{
if(hit.transform.tag == "Player")
Debug.Log("Exit Climbable edge");
}
}
"The closing date is six weeks later: April 17" <- isn't it over?
ā Khada