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 /
This question was closed Feb 13, 2016 at 11:08 PM by Demonlordd for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Demonlordd · Feb 12, 2016 at 09:51 PM · 2dreferencefindobjectoftype

Wont flip bullet with player

So my bullets are suppose to flip when my player is looking the other direction. This is how i've done it but somwhow it doesnt work, I've checked in debug so my player actually do get scale 0<... This is the script on the bullet

public class BulletDestroy : MonoBehaviour { public float speed; public Movement player = FindObjectOfType(); void start() {

     if (player.transform.localScale.x <0)
     { 
         speed = -speed;
     }
     

 }

 void FixedUpdate ()
 {
     GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);

} }

And heres the script for the movement/shooting and all that

using UnityEngine; using System.Collections;

public class Movement : MonoBehaviour { public GameObject bullet; public Transform firepoint;

 void FixedUpdate()
 {
     if (Input.GetKeyDown("space"))
     {
         FireBullet();
         anim.Play("Shoot");
     }
     if (GetComponent<Rigidbody2D>().velocity.x > 0)
     {
         transform.localScale = new Vector3(0.3f, 0.25f, 1f);
     }
     if (GetComponent<Rigidbody2D>().velocity.x < 0)
     {
         transform.localScale = new Vector3(-0.3f, 0.25f, 1f);
     }
 }

 public void FireBullet()
 {
     //Clone of the bullet
     Rigidbody2D Clone;
     Clone = GetComponent<Rigidbody2D>();

     //spawning the bullet at position
     Clone = (Instantiate(bullet, firepoint.position, firepoint.rotation)) as Rigidbody2D;
 }

}

Comment
Add comment · Show 1
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 Demonlordd · Feb 13, 2016 at 11:07 PM 0
Share

This is how i solved it, Completely stripped BulletDestroy of code (except the things about collision and actually destroying the bullet) And Changed my movement to this

     if (rb.transform.localScale.x < 0)
     {
       
        GameObject Clone = (GameObject)Instantiate(bullet, firepoint.position, firepoint.rotation);
         Clone.GetComponent<Rigidbody2D>().velocity = new Vector2(-20, 0) ;
         Debug.Log("You're looking Left");
     }
     else if (rb.transform.localScale.x > 0)
     {
         GameObject Clone = (GameObject)Instantiate(bullet, firepoint.position, firepoint.rotation);
         Clone.GetComponent<Rigidbody2D>().velocity = new Vector2(20, 0);
         Debug.Log("You're looking Right");
     }

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Winterblood · Feb 13, 2016 at 06:53 PM

For a start, don't have the bullet choose it's own facing. The bullet's job is just to fly straight until it hits something. Make FireBullet() set the bullet's velocity after instantiating it, and it will just keep going.

To get the sprite to flip, have FireBullet() add 180 degrees to firepoint.rotation if the player is facing -X.

Comment
Add comment · Show 1 · 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 Demonlordd · Feb 13, 2016 at 11:07 PM 0
Share

This is how i solved it, Completely stripped BulletDestroy of code (except the things about collision and actually destroying the bullet) And Changed my movement to this

     if (rb.transform.localScale.x < 0)
     {
       
        GameObject Clone = (GameObject)Instantiate(bullet, firepoint.position, firepoint.rotation);
         Clone.GetComponent<Rigidbody2D>().velocity = new Vector2(-20, 0) ;
         Debug.Log("You're looking Left");
     }
     else if (rb.transform.localScale.x > 0)
     {
         GameObject Clone = (GameObject)Instantiate(bullet, firepoint.position, firepoint.rotation);
         Clone.GetComponent<Rigidbody2D>().velocity = new Vector2(20, 0);
         Debug.Log("You're looking Right");
     }
avatar image
1

Answer by Arcade-Perfect · Feb 14, 2016 at 05:54 PM

as is a 2D sprite Unity added a field where you can flip X and/or Y on the SpriteRenderer Component. Can be the other way around when flipping, depending on the sprite orientation.

 if (GetComponent<Rigidbody2D>().velocity.x > 0)
      {
           GetComponent<SpriteRenderer>().flipX = false;
      }
      if (GetComponent<Rigidbody2D>().velocity.x < 0)
      {
           GetComponent<SpriteRenderer>().flipX = true;
      }


Same for Y flip value.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

MonoDevelop Unity API reference function with wrong path. How to Fix it? 1 Answer

c# Object reference not set to an instance of an object 1 Answer

Can't access a method from another script (Object reference not set to an instance of an object) 1 Answer

How do I reference int from other script 0 Answers

How do I access another script in Unity C# 2018.2? 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