I managed to screw up posting the code didn“t I? Somehow this forums run very slow in my browser.
Example 2:
//PlayerMovement and instantiating the projectile
public class PlayerController : MonoBehaviour {
public float maxSpeed;
private Rigidbody2D myRB;
private bool facingRight;
public Transform FirePoint;
public GameObject bullet;
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D>();
facingRight = true;
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw("Fire1") >0) fireRocket();
float move = Input.GetAxis("Horizontal");
myRB.velocity = new Vector2(move*maxSpeed, myRB.velocity.y);
if(move>0 && !facingRight)
{
flip();
} else if (move <0 && facingRight)
{
flip();
}
}
void flip()
{
facingRight=!facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void fireRocket()
{
if(facingRight)
{
Instantiate(bullet, FirePoint.position, Quaternion.Euler (new Vector3 (0,0,0)));
}else if (!facingRight)
{
Instantiate(bullet, FirePoint.position, Quaternion.Euler (new Vector3 (0,0,180f)));
}
}
}
//Projectile
public class Projectile : MonoBehaviour {
public float rocketSpeed;
Rigidbody2D myRB;
// Use this for initialization
void Awake () {
myRB = GetComponent<Rigidbody2D>();
if(transform.localRotation.z>0)
myRB.AddForce(new Vector2(-1,0) * rocketSpeed, ForceMode2D.Impulse);
else myRB.AddForce(new Vector2(1, 0) * rocketSpeed, ForceMode2D.Impulse);
}
// Update is called once per frame
void Update () {
}
}
According to your answer, I guess the best thing to do is to work with facing direction bools for like every single direction? (8 since I want to instantiate the projectile basically in 45 degrees for every direction)
By calling a parent you mean, the Child (firePoint in my case) would be moving with the direction the player is facing? Would be great, if that worked out.
Apparently, I canĀ“t even manage to make my character ālookā Up and down as of now.
I probably don“t fully understand the whole direction Concept of Unity.
The second thing I don“t get about this is, how do I make my character move in the correct direction, to recognize, which angle should be used for the projectile to fire?
If something is unclear, pls ask. I know, this got fuzzy. Especially since I posted 2 Codes at once. I would be supergrateful if one thing worked out. I think, I get something terribly wrong with directions and parenting stuff/facing directions.
Thanks for your answer!