Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SebastianRSP · Jun 19, 2014 at 01:42 PM · c#2danimatorcharacter

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;
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanPaterson · Jun 19, 2014 at 02:05 PM 1
Share

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 ();
  
 } 
 
     }
  
avatar image SebastianRSP · Jun 19, 2014 at 02:19 PM 0
Share

@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 :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanPaterson · Jun 21, 2014 at 03:03 PM 0
Share

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

avatar image SebastianRSP · Jun 23, 2014 at 09:23 PM 0
Share
 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges