Last active
December 11, 2015 12:18
-
-
Save SteveSwink/4599445 to your computer and use it in GitHub Desktop.
broken grapple stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class GPlayerLogic : MonoBehaviour { | |
public GameObject grappleHitParicle; | |
public float speed = 6.0F; | |
public float jumpBurst = 8.0F; | |
public float gravity = 20.0F; | |
public float ropeForce = 100.0f; | |
public Transform grappleShot; | |
public Transform grappleGun; | |
CharacterController cc; | |
Vector3 center; | |
Vector3 playerVelocity = Vector3.zero; | |
bool canFireGrapple = true; | |
bool grappleConnected = false; | |
//The state manager | |
private StateSystem stateSystem; | |
//The state when grapple is connected | |
public State grappleState; | |
//The normal walking state | |
public State walkState; | |
// Debug linerenderer for grapplenootch | |
Color c1 = Color.blue; | |
Color c2 = Color.red; | |
int lengthOfLineRenderer = 2; | |
LineRenderer lrBlue, lrRed; | |
public Vector3 swingSpeed = new Vector3(0,0,0); | |
public float swingGravity = 0.1f; | |
float swingDistance; | |
GameObject anchorPoint = null; | |
public void Awake() { | |
//Fetch the state manager from the editor | |
stateSystem = GetComponent<StateSystem>(); | |
stateSystem.name = "GrapplePlayerLogic"; | |
//Add the states to the state machine, | |
//One method of doing this is passing them via editor (using game objects as script hosts or prefabs) | |
stateSystem.AddState(grappleState); | |
stateSystem.AddState(walkState); | |
anchorPoint = (GameObject)Instantiate(GameObject.CreatePrimitive(PrimitiveType.Sphere), new Vector3(0,0,0), Quaternion.identity); // as Transform; | |
} | |
void Start () { | |
cc = GetComponent<CharacterController>(); | |
// catch the grapple when it connects | |
EventRouter.Subscribe("GrappleConnected", GrappleConnect); | |
// linerenderer setups | |
lrBlue = gameObject.AddComponent<LineRenderer>(); | |
lrBlue.material = new Material(Shader.Find("Particles/Additive")); | |
lrBlue.SetColors(c1, c1); | |
lrBlue.SetWidth(0.2F, 0.2F); | |
} | |
void GrappleConnect(EventRouter.Event evt){ | |
// get the connection point of the grapple | |
var gp = evt.GetData<Vector3>(0); | |
anchorPoint.transform.position = gp; | |
swingDistance = (transform.position - anchorPoint.transform.position).magnitude; | |
swingDistance *= 0.75f; | |
swingSpeed = new Vector3(0.1f,0,0); | |
Instantiate(grappleHitParicle, gp, Quaternion.identity); | |
grappleConnected = true; | |
stateSystem.SetState("State_Grapple", transform); | |
Debug.Log("grapple connect"); | |
} | |
void Update () { | |
if (cc.isGrounded && !grappleConnected) { | |
playerVelocity.x = Input.GetAxisRaw("Horizontal") * speed; | |
playerVelocity = transform.TransformDirection(playerVelocity); | |
} | |
//ToDo: on enter grapple state, set speed to be current cc velocity | |
if(grappleConnected){ | |
swingSpeed.y -= swingGravity; // * Time.deltaTime; | |
Vector3 dir = transform.position - anchorPoint.transform.position; | |
swingSpeed += dir.normalized * swingDistance - dir; | |
// Vector3 desired = transform.position + swingSpeed; | |
cc.Move(swingSpeed); | |
// cc.SimpleMove(swingSpeed); | |
}else{ | |
// Factor in gravity | |
playerVelocity.y -= gravity * Time.deltaTime; | |
// Actually move the CharacterController | |
cc.Move(playerVelocity * Time.deltaTime); | |
} | |
if(Input.GetButtonDown("FireGrapple")){ | |
if(canFireGrapple) | |
FireGrapple(); | |
} | |
} | |
void FireGrapple(){ | |
var shot = Instantiate(grappleShot,grappleGun.transform.position, grappleGun.transform.rotation) as Transform; | |
// shot.rigidbody.velocity += new Vector3(playerVelocity.x,0,0); | |
shot.transform.parent = transform; | |
// var px = new Vector3(playerVelocity.x, 0,0); | |
//shot.rigidbody.AddForce(px); | |
Physics.IgnoreCollision(transform.collider, shot.collider); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment