Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Oct 28, 2020 at 06:54 PM by Treven for the following reason:

I figured out where I went wrong. and now works.

avatar image
0
Question by Treven · Sep 16, 2020 at 07:57 PM · projectilemathffliplocalscaleflipping

Having trouble with flipping my projectile.

Hello my name is Treven and am trying to flip the local scale of my projectile when shot to match my player's direction. I have a simple instantiation for my projectile but when I put in this piece of code, the projectile stretches and turns invisible while the scale is told to be the same. I have made a separate scene and scripts to try and figure this out but no luck.

Heres what I got to flip my projectile.

Flip Code

         if (Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon)
         {
             transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), myRigidbody.velocity.y);
         }

What is supposed to happen is to use the object's rigid body to see if the object is moving then to get the transform's local scale (X) to the have it be positive or negative depending on direction.

But what happens is the projectile turns invisible, the scale dose not change, and the scale of the object gets stretched.

The projectile works like intended until I put in the script to try flipping the projectile.

Any help in finding a solution would be much appreciated. Full Code and Player Script Below:

Player Control Script

 public class SimplePlayertesterScript : MonoBehaviour
 {
     [Header("Player Movement")]
     [SerializeField] float playerWalkSpeed = 5f;
 
     [SerializeField] GameObject projectileChargeBuster = null;
     [SerializeField] GameObject projectilePrefab = null;
 
     bool hasHorizontalSpeed()
     {
         if (Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 
     [SerializeField] public float hori;
     [SerializeField] float vert;
 
     Rigidbody2D myRigidbody;
 
     // Start is called before the first frame update
     void Start()
     {
         myRigidbody = GetComponent<Rigidbody2D>();
     }
 
     void Update()
     {
         hori = Input.GetAxisRaw("Horizontal");
         vert = Input.GetAxisRaw("Vertical");
 
         Vector2 playerVelocity = new Vector2(playerWalkSpeed * hori, myRigidbody.velocity.y);
         myRigidbody.velocity = (playerVelocity);
 
         if (hasHorizontalSpeed())
         {
             if (Mathf.Abs(hori) > 0)
             {
                 transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), transform.localScale.y);
             }
         }
 
         if (Input.GetKey("z"))
         {
             SpawnStandardProjectile();
         }
     }
 
     void SpawnStandardProjectile()
     {
         GameObject projectile = Instantiate(projectilePrefab, projectileChargeBuster.transform.position, projectileChargeBuster.transform.rotation) as GameObject;
 
         if (transform.localScale.x >= 0)
         {
             projectile.GetComponent<SimpleProjectileTest>().projectileMoveSpeed *= 1;
         }
         else
         {
             projectile.GetComponent<SimpleProjectileTest>().projectileMoveSpeed *= -1;
         }
     }
 }

Spawning the Projectile:

 public class SimpleProjectileTest : MonoBehaviour
 {
     [SerializeField] public float projectileMoveSpeed = 3;
     [SerializeField] float projectileLifeTime = 3;
 
     Rigidbody2D myRigidbody;
 
     // Start is called before the first frame update
     void Start()
     {
         //transform.parent = FindObjectOfType<ContainerManager>().projectileContainer.transform;
 
         myRigidbody = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         myRigidbody.velocity = transform.right * projectileMoveSpeed;
 
 
         if (Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon)
         {
             transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), myRigidbody.velocity.y);
         }
 
 
         ProjectileLife();
     }
 
     private void ProjectileLife()
     {
             Destroy(gameObject, projectileLifeTime);
     }
 }



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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

134 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lock character's LocalScale during Idle animation 1 Answer

Flipping ( localscale/rotate unfitting ) 0 Answers

Problem with counting front flips 1 Answer

Script to flip a sprite over the x axis not working? C#, Unity2D 1 Answer

,Why Character Is not Flipping ? There is some problem i am not able to see 0 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