- Home /
Having multiple 2D Characters
Hey there, I followed the 2D Character Controller tutorial, and created a 2D Character. I am trying to achieve an effect, where you press a button, and it switches to another character in the scene, and you can then control the second character, while the first one is standing still. I can achieve this effect, but I am having some different problems, first is, that the first character, is still doing the running animation when I am switching, I already got the camera working so that it switches. Any help is appreciated, and it is in C# :D
Here is my code for the Player Controller: using UnityEngine; using System.Collections;
public class PlayerControllerScript1 : MonoBehaviour {
public GameObject camera;
public float jumpForce;
public float maxSpeed;
bool facingRight = true;
Animator anim;
//on ground and so on
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
void Start () {
anim = GetComponent<Animator>();
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
//Walking Animation and Movement
float move = Input.GetAxis ("Horizontal");
anim.SetFloat("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
//Flipping the Character
if(move > 0 && !facingRight)
Flip ();
else if(move < 0 && facingRight)
Flip ();
}
void Update(){
if(grounded && Input.GetKeyDown(KeyCode.Space)){
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
}
void Flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
It's probably not the best way to go about it, but you could try an if statement to see weather the player is active/moving? So, e.g. when you switch to the other character, just set this ones 'active' to false
bool active = true;
void FixedUpdate () {
if(active == true)
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
//Walking Animation and $$anonymous$$ovement
float move = Input.GetAxis ("Horizontal");
anim.SetFloat("Speed", $$anonymous$$athf.Abs (move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
//Flipping the Character
if(move > 0 && !facingRight)
Flip ();
else if(move < 0 && facingRight)
Flip ();
}
}
@RyanPaterson This does work, but only halfway, the problem is that if the player is "falling" While it is switched, the falling animation keeps occuring when hitting the ground. Thanks for the help though :)
Answer by SebastianRSP · Jun 21, 2014 at 02:23 PM
Would anyone be able to help me? I got the basics to work, which means that when that bool is not active, you cannot control the character anymore, I was then able to do so that the animations floats and bools, like Speed and Vspeed was set to 0, which means that he will not move anymore. Now, I have this line of code:
else {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", 0);
anim.SetFloat ("Speed", 0);
}
But, I can still jump with the character, why is this?
As a guess, You have your jump function inside of Update unaffected, you'll need to add the same if statement to it that you have in your fixedupdate, to stop it from moving.
If not, post your new script and people'l be more likely to help
using UnityEngine;
using System.Collections;
public class PlayerControllerScript1 : $$anonymous$$onoBehaviour {
public GameObject camera;
public float jumpForce;
public float maxSpeed;
public bool followyes;
bool facingRight = true;
Animator anim;
//on ground and so on
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public Layer$$anonymous$$ask whatIsGround;
void Start () {
anim = GetComponent<Animator>();
}
void FixedUpdate () {
if (followyes) {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
//Walking Animation and $$anonymous$$ovement
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", $$anonymous$$athf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
//Flipping the Character
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
} else {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", 0);
anim.SetFloat ("Speed", 0);
}
}
void Update(){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)){
followyes = false;
}
if(grounded && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
}
void Flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Here i s my entire script, the animations stop, but I can still jump, any help is appreciated :D
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Start sprite animation at the same index that the previous animation stopped 2 Answers
How would I switch between 2 animation states connected to 1 state under input? 0 Answers
How to change the sprite and the animator in script to make a character selection? 2 Answers
Android 2D Character Controller 2 Answers