The question is answered, right answer was accepted
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;
}
}
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");
}
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.
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");
}
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.
Follow this Question
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