Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by wolfkillshepard · May 02, 2017 at 10:25 PM · platformercharacter controllerflipping

Help apdapting character flip script to use GetComponent().flip = true;

Hello! Thanks in advance for the help and my apologies for a probably pretty newbie question but been struggling to figure this out properly. I've been learning how to make character controllers for a platformer and it's been going good but I am a much better artist and animator than a programmer. Need some help adapting the movement script to use GetComponent<Puppet2D_GlobalControl>().flip = true; as my character flipping method.

I am using a skeletal animation solution called Puppet2d and in order for flipping assets to work properly I must use that component I mentioned above, otherwise the bone positioning gets screwed up.

Here's my script below before I add the component to it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class spacemancontroller : MonoBehaviour {
     //how fast the character can move
     public float topSpeed = 10f;  
     // tell the sprite which way it is facing
     bool facingRight = true;
 
     //physics in fixed update
     void FixedUpdate()
     {
         //get move direction
         float move = Input.GetAxis ("Horizontal");
 
         //add velocity to the rigidbody in the move direction * our speed
         GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y); 
 
 
         //if were facing the neg direction and not right, flip 
         if (move > 0 && !facingRight)
             Flip();
         else if (move < 0 && facingRight)
             Flip();
     }
 
     void Flip()
     {
         //saying we are facing the opposite directions
         facingRight = !facingRight;
 
             //get the local scale
             Vector3 theScale = transform.localScale;
 
         //flip on the x axis
         theScale.x *= -1;
 
         //reapply that to the level scale
         transform.localScale = theScale;
     }
 }
 

I've been reading over the script trying to make sure I understand exactly what's happening at each line but I think I'm misunderstanding part of it because when I replace the instances of Flip(); with GetComponent<Puppet2D_GlobalControl>().flip = true; I can get the character to properly turn around once but I can't get him to turn around again.

Thanks again for any help you guys can provide, I've been sitting on this for many days without any luck.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by wolfkillshepard · May 03, 2017 at 12:19 AM

Thanks for the reply! That makes sense, I'll change that and see if I can get it working when in back home in a couple hours. I'll try caching it too after I get it working.

Update, Solved:

I changed my if statement to use:

         Puppet2D_GlobalControl _globalControl = gameObject.GetComponent<Puppet2D_GlobalControl> ();
 
         if (Input.GetAxis (   "Horizontal"    ) < 0)
         {
             _globalControl.flip = true;
 
 
         } 
         else if (Input.GetAxis ("Horizontal" ) > 0)
         {
 
             _globalControl.flip = false;
 
 
         } 

character now flips back and forth just fine. Anyone using puppet2D would need to do something like this for their flipping control.

Comment
Add comment · 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
0

Answer by Jawchewa · May 03, 2017 at 12:15 AM

I'm not really experienced with Puppet2D, but from what your describing, I think you'll want to replace GetComponent<Puppet2D_GlobalControl>().flip = true; with GetComponent<Puppet2D_GlobalControl>().flip = !GetComponent<Puppet2D_GlobalControl>().flip;

From what you were describing, it sounds like you were always setting the boolean value of flip to true, instead of toggling the value. Also, unrelated, but I would probably recommend caching the Puppet2D_GlobalControl component in a variable, so that you don't have to keep calling GetComponent.

Comment
Add comment · 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

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

128 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 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 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 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 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 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

Switching between many characters and controllers,How to switch between many character controllers and sprites 0 Answers

Rigidbody2D.velocity Interupts Rigidbody2D.AddForce in 2D Platformer 0 Answers

How to address a 2D animation flaw during a ledge climb? 0 Answers

Need help: Playmaker Physics Based Platformer 0 Answers

Should i use rigidbody for platformer movement? 1 Answer


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