Coming soon - Get a detailed view of why an account is flagged as spam!
view details
2
Help with implementing color changes
Post Body

Hello,

I am a first time game creator who is making an Asteroids game for a school project. Everything is going well, but I'm trying to implement a mechanic in which there are two different colors for asteroids, and two different colors for the ship's bullets. Bullets can only affect like-colored Asteroids, but not different colored ones.

My problem is that I've set up my code so that every time the player presses "S", a counter increases by one. If the counter is divisible by 2, then the bullets should be white. If not, they should be black.

The bullets do change color, but there are many times when I press the S button, but the bullets don't change color.

Code: (I'm not very good at this, but I tried my best)

using UnityEngine; using System.Collections;

public class ShipMovement : MonoBehaviour {

public float RotationSpeed;
public float Thrust;
public ParticleSystem Particle;
public GameObject BulletW;
public GameObject BulletB;
public float ShipColor;

// Use this for initialization
void Start() 
{
    ShipColor = 2;
}

// Update is called once per frame
void Update() 
{


    if (Input.GetKeyDown (KeyCode.Space)) {
        if (ShipColor % 2 == 0)
            Instantiate (BulletW, transform.position, transform.rotation);
        else
            Instantiate (BulletB, transform.position, transform.rotation);
    }


}

void FixedUpdate()
{
    if (Input.GetKey (KeyCode.S))
        ShipColor  = 1;
    if (Input.GetKey (KeyCode.A)) {
        rigidbody2D.angularVelocity = RotationSpeed;
    } else if (Input.GetKey (KeyCode.D)) {
        rigidbody2D.angularVelocity = -RotationSpeed;
    } else {
        rigidbody2D.angularVelocity = 0;
    }


    if (Input.GetKey (KeyCode.W)) {
        rigidbody2D.AddForce (transform.up * Thrust);
        Particle.Play ();
    } else {
        Particle.Stop ();
    }




}

}

BulletW = White bullets and BulletB = black bullets

I originally intended the ship to change color as well (hence the name of the variable) but I have since moved away from that idea.

The Particle is an exhaust-like system that emanates from the back of the ship.

Thrust and Rotation affect the ship's speed and which it rotates.

Hopefully, some one can help me figure this out, and soon, since if I can't figure out what's wrong, I'll have to switch projects.

Thank you!

Author
Account Strength
100%
Account Age
10 years
Verified Email
Yes
Verified Flair
No
Total Karma
74,381
Link Karma
1,224
Comment Karma
72,834
Profile updated: 4 days ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
9 years ago