Hi
I’m fairly new to Unity, I’ve come across an odd bug where my hero is no longer falling or reacting to gravity while the animator component is attached.
This occurred after I added a wolf child object to a parent class called Character also shared by the hero.
The odd thing is, if I remove/delete the wolf from the game scene and hierarchy the bug still persists, so I dont believe its to do with inheritance, but I may be wrong.
If I remove the animator, the character reacts to physics, but immediately returns to his original position stuck in midair once I re-add the animator component.
Here’s a 1 demonstrating the issue, and below is a bunch of code from the Character.cs(parent class)
I’m hoping its just a silly mistake on my part, but I cant seem to spot it. Also please note I’m using Unity 5.4.1 and I’ve also tried it with the 5.5 beta.
Kind Regards
Sat
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour {
//protected Animator anim;
public float speedX =6.0f;
protected float speed;
public float punchSpeedX;
public float jumpSpeedY;
public int TotalGroundObjectsTouching;
protected bool FacingRight, Jumping, Moving;
public int JumpLimit, HasJumped;
public bool isPunching;
Rigidbody2D rb;
public Animator MyAnimator { get; private set; }
// Use this for initialization
public virtual void Start () {
MyAnimator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
FacingRight = true;
TotalGroundObjectsTouching=0;
Moving = false;
punchSpeedX= 1.5f;
isPunching = false;
}
// Update is called once per frame
public virtual void Update () {
//Debug.Log("SPEED IS "+speed);
MovePlayer(speed);
}
void Flip()
{
Vector3 temp = transform.localScale;
temp.x *=-1;
transform.localScale = temp;
}
public void touchedGround(int collCount)
{
HasJumped=0;
TotalGroundObjectsTouching = collCount;
Jumping = false;
MyAnimator.SetInteger("TotalGroundObjectsTouching",TotalGroundObjectsTouching);
}
public virtual void setGroundObjects(int amountTouching)
{
if( HasJumped ==0 && TotalGroundObjectsTouching==1)
{
//HasJumped++;
}
TotalGroundObjectsTouching=amountTouching;
MyAnimator.SetInteger("TotalGroundObjectsTouching",TotalGroundObjectsTouching);
}
protected void MovePlayer(float playerSpeed)
{
Debug.Log("move player "+playerSpeed);
if(isPunching==false)
{
rb.velocity = new Vector3 (playerSpeed, rb.velocity.y,0);
//rb.velocity = new Vector3 (speed, Mathf.Clamp(rb.velocity.y, -18.0F, 20.0F) ,0);
}else{
int direction;
if(FacingRight==true)
{
direction= 1;
}else{
direction=-1;
}
rb.velocity = new Vector3 ((speedX*punchSpeedX)*direction, 0,0);
}
}
public virtual void MoveLeft()
{
//if(isPunching==false)
//{
//moveWhilePunch="false";
Moving = true;
//LeftMoveKeyDown=true;
speed = -speedX;
if(FacingRight==true)
{
FacingRight = false;
Flip();
}
//}else{
//moveWhilePunch = "left";
//}
}
public virtual void MoveRight()
{
//if(isPunching==false)
//{
//moveWhilePunch="false";
Moving = true;
//RightMoveKeyDown=true;
speed = speedX;
if(FacingRight==false)
{
FacingRight = true;
Flip();
}
//}else{
//moveWhilePunch="right";
//}
}
public void Punch()
{
//Debug.Log("Punch event");
isPunching=true;
MyAnimator.SetBool("isAttacking",isPunching);
}
public void StopPunching() // this is called by the HeroPunchingState.cs at the end of punch animation
{
//Debug.Log("STOP PUNCHING");
isPunching=false;
///punchStarted=false;
MyAnimator.SetBool("isAttacking",false);
}
public void StopMoving()
{
if(isPunching==false)
{
Moving = false;
speed = 0;
}
}
public Vector2 GetDirection()
{
return FacingRight ? Vector2.right : Vector2.left;
}
public bool IsFacingRight()
{
return FacingRight;
}
public void Jump()
{
if(HasJumped<JumpLimit){
HasJumped++;
Jumping=true;
//rb.AddForce(new Vector2 (rb.velocity.x, jumpSpeedY));
rb.velocity= new Vector2 (rb.velocity.x, jumpSpeedY);
}
}
}