Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- public class Movement : MonoBehaviour {
- public bool Ragdoll = false;
- public int RagdollReset = 0;
- public Rigidbody ChildRB;
- public Collider ChildCD;
- public List<Rigidbody> ChildRBS;
- public Animator Animator;
- public Rigidbody Body;
- public List<Collider> ChildCDS;
- public int Change = 0;
- [Header("Parameters")]
- public float ForwardSpeed;
- public float LateralSpeed;
- public float BackwardSpeed;
- // Use this for initialization
- void Start () {
- List<Collider> ChildCDS = new List<Collider>(GetComponentsInChildren<Collider>());
- List<Rigidbody> ChildRBS = new List<Rigidbody> (GetComponentsInChildren<Rigidbody> ());
- ChildCDS.Remove (this.GetComponent<Collider>());
- ChildRBS.Remove (this.GetComponent<Rigidbody>());
- Animator = GetComponent<Animator> ();
- Body = GetComponent<Rigidbody>();
- }
- public virtual void MoveForward() {
- Body.MovePosition(transform.position + transform.forward * ForwardSpeed);
- }
- public virtual void MoveBackward() {
- Body.MovePosition(transform.position - transform.forward * BackwardSpeed);
- }
- public virtual void MoveLeft() {
- this.transform.Rotate (0, LateralSpeed * -1, 0);
- }
- public virtual void MoveRight() {
- this.transform.Rotate (0, LateralSpeed * 1, 0);
- }
- // Update is called once per frame
- void Update () {
- List<Collider> ChildCDS = new List<Collider>(GetComponentsInChildren<Collider>());
- List<Rigidbody> ChildRBS= new List<Rigidbody> (GetComponentsInChildren<Rigidbody>());
- if (Input.GetKey(KeyCode.W) && Ragdoll == false) {
- MoveForward ();
- }
- if (Input.GetKey(KeyCode.S) && Ragdoll == false) {
- MoveBackward ();
- }
- float Move = Input.GetAxis ("Vertical");
- Animator.SetFloat ("Speed", Move);
- RagdollReset++;
- if (Input.GetKeyDown (KeyCode.E) && RagdollReset > 1 && Ragdoll == true) {
- Ragdoll = false;
- RagdollReset = 0;
- Change = 0;
- }
- if (Input.GetKeyDown (KeyCode.E) && RagdollReset > 1 && Ragdoll == false || Input.GetKeyDown (KeyCode.Space) && RagdollReset > 1 && Ragdoll == false) {
- Ragdoll = true;
- RagdollReset = 0;
- Change = 0;
- }
- if (Ragdoll == false && Change < 1) {
- Change++;
- foreach (Rigidbody ChildRB in ChildRBS) {
- ChildRB.isKinematic = true;
- }
- foreach (Collider ChildCD in ChildCDS) {
- ChildCD.enabled = false;
- }
- Animator = this.GetComponent<Animator> ();
- Animator.enabled = true;
- } else if (Change < 1) {
- Change++;
- foreach (Rigidbody ChildRB in ChildRBS) {
- ChildRB.isKinematic = false;
- }
- foreach (Collider ChildCD in ChildCDS) {
- ChildCD.enabled = true;
- }
- Animator.enabled = false;
- }
- }
- }
- // TODO : @Mopzilla
- // Add velocity to Actor. You'll want it to increase on Update if you're moving forward and decrease if you're not. Use the velocity to scale body_.MovePosition's argument.
- // Make sure you keep current velocity and velocity from last frame.
- // When you send your guy into ragdoll mode, do this:
- // acceleration = (this frame's velocity - last frame's velocity) / time.DeltaTime
- // body_.AddForce(mass of force * acceleration, ForceMode.Impulse);
- // There are a lot of ways you could do velocity, like sampling a curve, using lerp, etc. I'll leave that for you to experiment.
- // To add turning, in the move left/right functions instead of doing what I did by moving their position due west/east, you would continue to move your character forward and a little bit to the left/right.
- // You can get really creative with turning by using Lerp and a smoothing function to make him turn a little bit, then gradually more, for realism.
- // Message me if you need any help!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement