Unity Script issues

A place for tutorials on how to get the most out of Flash

Unity Script issues

Postby hat973 » Mon Feb 13, 2017 10:20 pm

So I'm still getting the hang of this annoying script deal I don't know what went wrong but I debug and it's my Jump I have Jump behavior that states my animator.Instance.Jump = true; then on exit the same thing but false. on my landing behavior there's a reset trigger that works but the problem is I can't seam to Jump witch is my target I was able to fix it last time but I don't know what I did to break it can some one help me?

Spoiler (click to show/hide):

Code: Select All Code
using UnityEngine;
using System.Collections;

public class Player : Character
{

    private static Player instance;

    public static Player Instance
    {
        get
        {
            if (instance == null)
            {
                instance = GameObject.FindObjectOfType<Player>();
            }
            return instance;
        }
    }

    [SerializeField]
    private Transform[] groundPoints;

    [SerializeField]
    private float groundRadius;

    [SerializeField]
    private LayerMask whatIsGround;

    [SerializeField]
    private bool airControl;

    [SerializeField]
    private float jumpForce;

    public Rigidbody2D MyRigidbody { get; set; }

    public bool Attack { get; set; }

    public bool Slide { get; set; }

    public bool Jump { get; set; }

    public bool OnGround { get; set; }

    private Vector2 startPos;

    // Use this for initialization
    public override void Start()
    {
        Debug.Log("PlayerStart");
        base.Start();
        startPos = transform.position;
        MyRigidbody = GetComponent<Rigidbody2D>();
               
    }

    void Update()
    {
        HandleInput();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");

        OnGround = IsGrounded();

        HandleMovement(horizontal);

        Flip(horizontal);

        HandLayers();

    }

    private void HandleMovement(float horizontal)
    {

       if (MyRigidbody.velocity.y < 0)
        {
            myAnimator.SetBool("Land", true);
        }
       if(!Attack && !Slide && (OnGround || airControl))
        {
            MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
        }
       if (Jump && MyRigidbody.velocity.y == 0)
        {
            MyRigidbody.AddForce(new Vector2(0, jumpForce));
        }
        myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
    }
    private void HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            myAnimator.SetTrigger("Jump");
        }
        if (Input.GetKeyDown(KeyCode.Z))
        {
            myAnimator.SetTrigger("Attack");
        }
       if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            myAnimator.SetTrigger("Slide");   
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
            myAnimator.SetTrigger("Throw");
        }
       
    }

    private void Flip(float horizontal)
    {
       if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            ChangeDirection();
        }
    }

    private bool IsGrounded()
    {
        if (MyRigidbody.velocity.y <= 0)
        {
            foreach (Transform point in groundPoints)
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

                for (int i = 0; i < colliders.Length; i++)
                {
                    if (colliders[i].gameObject != gameObject)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private void HandLayers()
    {
        if (!OnGround)
        {
            myAnimator.SetLayerWeight(1, 1);
        }
        else
        {
            myAnimator.SetLayerWeight(1, 0);
        }
    }

    public override void ThrowKnife(int value)
    {
        if (!OnGround && value == 1 || OnGround && value == 0)
        {
            base.ThrowKnife(value);
        }
    }
}
Red light panda

:ugeek: Tumbler: Tumbler
:geek: I have a Blog: Red Light Blog
;) I stream to Give me a Watch over at: Picarto
:mrgreen: Donate: Patreon
:roll: Discord:Discord
User avatar
hat973
 
Joined: Tue Apr 24, 2012 12:44 am

Re: Unity Script issues

Postby BlueLight » Wed Feb 15, 2017 3:08 am

You know, it's funny. I've used Unity as a hobbist for years, and never bothered to learn the Animator...
Anyways, checking script now. I'll see if i can find a logic error.

Is there any script outside of Player that changes values or calls method/functions?

EDIT 1
Code: Select All Code
 private void HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            myAnimator.SetTrigger("Jump");
        }
        if (Input.GetKeyDown(KeyCode.Z))
        {
            myAnimator.SetTrigger("Attack");
        }
       if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            myAnimator.SetTrigger("Slide");   
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
            myAnimator.SetTrigger("Throw");
        }
       
    }


Is SetTrigger the right call? This sounds like you're creating the trigger you need to call. Then again i'd expect a second parameter if it was what i was suggesting.

EDIT 2

Can you take a screen shot of the animator window? I think that's the right one. I don't think this is a problem with your script. At least there isn't anything which detect when the jump animation should end, then well ends it.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Unity Script issues

Postby hat973 » Wed Feb 15, 2017 7:51 am

I've been fallowing this platform tut who put behaviors on the animation all they do is reset something or other like for the Attack behavior (Code in spoiler) looks like this.

Spoiler (click to show/hide):

Code: Select All Code
public class AttackBehaviour : StateMachineBehaviour {

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Player.Instance.Attack = true;

        if (Player.Instance.OnGround)
        {
            Player.Instance.MyRigidbody.velocity = Vector2.zero;
        }
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Player.Instance.Attack = false;
        animator.ResetTrigger("Attack");
        animator.ResetTrigger("Throw");
    }

    // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}
}


This Resets the Triggers for my Attacks and so on The Land behavior I have for when the player is heading to the ground is the same but on the OnstateUpdate the code looks like this.

Spoiler (click to show/hide):

Code: Select All Code
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
   if (Player.Instance.OnGround)
        {
            animator.SetBool("Land", false);
            animator.ResetTrigger("Jump");
        }
   }


That's the only code on the land behavior. Slide code looks like this.
Spoiler (click to show/hide):

Code: Select All Code
using UnityEngine;
using System.Collections;

public class SlideBehaviour : StateMachineBehaviour {

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Player.Instance.Slide = true;
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Player.Instance.Slide = false;
        animator.ResetTrigger("Slide");
    }

    // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}
}


That's really all the outside code the rest don't even effect the player directly just platform code a camera code and that's about it...outside the charater script witch is just a Badguy AI that the player can use from time to time. Oh and down below are the pics you want for the animation.

Spoiler (click to show/hide):

animation bord 1.png
Main frame for player

animation bord 2.png
Jumping there's a layer called Air layer.

animation bord 3.png
Jump Attack these are the same layer as the Jump.



Edit: Here's that Tut by the by I only got up to 14.2 on t he rewatch. http://tinyurl.com/jaq7zpu
Red light panda

:ugeek: Tumbler: Tumbler
:geek: I have a Blog: Red Light Blog
;) I stream to Give me a Watch over at: Picarto
:mrgreen: Donate: Patreon
:roll: Discord:Discord
User avatar
hat973
 
Joined: Tue Apr 24, 2012 12:44 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users