Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class ThirdPersonCharaController : MonoBehaviour {
- // Objects
- public GameObject PlayerHolder;
- // Animation Details
- public AnimationClip idleAnimation;
- public AnimationClip walkAnimation;
- public float walkMaxAnimationSpeed = 0.75f;
- // the movement Details
- public float gravity = 20.0f;
- public float moveSpeed = 10.0f;
- public float RotateSpeed = 0.1f;
- public bool LockMovement = false;
- // Private Fields
- private Animation _animation;
- private CharacterController controller;
- private Vector3 moveDirection = Vector3.zero;
- private Vector3 moveVelocity;
- private float oldgravity;
- private float oldRotateSpeed;
- private float new_direction = 0.0f;
- private float rotate_t = 0.0f;
- //=====================================================================
- // Use this for initialization
- void Start () {
- oldRotateSpeed = RotateSpeed;
- oldgravity = gravity;
- _animation = PlayerHolder.GetComponent<Animation>();
- controller = transform.gameObject.GetComponent<CharacterController>();
- if(!_animation)
- Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");
- if(!idleAnimation) {
- _animation = null;
- Debug.Log("No idle animation found. Turning off animations.");
- }
- if(!walkAnimation) {
- _animation = null;
- Debug.Log("No walk animation found. Turning off animations.");
- }
- }
- //=====================================================================
- // Updated every Frame
- void Update() {
- if(_animation != null){
- _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
- _animation.CrossFade(walkAnimation.name);
- }
- if(controller != null && LockMovement == false){
- if (controller.isGrounded) { // Gelandet
- Debug.Log("gelandet");
- moveVelocity = new Vector3(0,0,0);
- if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) // Rechts
- {
- if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)){ // Für Diagonale
- rotate_t = 0.0f;
- new_direction = 45;
- moveVelocity = new Vector3(1,0,1);
- }
- else if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)){
- rotate_t = 0.0f;
- new_direction = 135;
- moveVelocity = new Vector3(1,0,-1);
- }
- else{
- rotate_t = 0.0f;
- new_direction = 90;
- moveVelocity = new Vector3(1,0,0);
- }
- }
- else if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) // Links
- {
- if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)){ // Für Diagonale
- rotate_t = 0.0f;
- new_direction = 315;
- moveVelocity = new Vector3(-1,0,1);
- }
- else if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)){
- rotate_t = 0.0f;
- new_direction = 225;
- moveVelocity = new Vector3(-1,0,-1);
- }
- else{
- rotate_t = 0.0f;
- new_direction = 270;
- moveVelocity = new Vector3(-1,0,0);
- }
- }
- else if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) // Hoch
- {
- if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)){ // Für Diagonale
- rotate_t = 0.0f;
- new_direction = 315;
- moveVelocity = new Vector3(-1,0,1);
- }
- else if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)){
- rotate_t = 0.0f;
- new_direction = 45;
- moveVelocity = new Vector3(1,0,1);
- }
- else{
- rotate_t = 0.0f;
- new_direction = 0;
- moveVelocity = new Vector3(0,0,1);
- }
- }
- else if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) // Runter
- {
- if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)){ // Für Diagonale
- rotate_t = 0.0f;
- new_direction = 255;
- moveVelocity = new Vector3(-1,0,-1);
- }
- else if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)){
- rotate_t = 0.0f;
- new_direction = 135;
- moveVelocity = new Vector3(1,0,-1);
- }
- else{
- rotate_t = 0.0f;
- new_direction = 180;
- moveVelocity = new Vector3(0,0,-1);
- }
- }
- moveDirection = moveVelocity;
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= moveSpeed;
- gravity = oldgravity;
- }
- else{ // FÄLLT NOCH
- Debug.Log("fällt mit " + gravity);
- gravity += 5.0f;
- gravity = Mathf.Clamp(gravity, oldgravity, oldgravity*2);
- }
- Rotate_to();
- if(Mathf.Abs(PlayerHolder.transform.eulerAngles.y-new_direction) < 35){
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- }
- }
- }
- //=====================================================================
- float f;
- void Rotate_to(){
- rotate_t += Time.deltaTime * RotateSpeed;
- f = PlayerHolder.transform.eulerAngles.y;
- if (f > 180.0f){
- f -= 360.0f;
- }
- PlayerHolder.transform.eulerAngles = new Vector3(0, Mathf.Lerp(f, new_direction, rotate_t), 0);
- }
- // Vector3 myRotation = Vector3.zero;
- // void Rotate_to(){
- // rotate_t += Time.deltaTime * RotateSpeed;
- // myRotation.y = Mathf.Lerp(myRotation.y, new_direction, rotate_t);
- // PlayerHolder.transform.eulerAngles = myRotation;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement