Unity - Tilting and Shooting

Tilting

A public variable tilt was created.

public float tilt;


Within the fixedupdate rigidbody rotation was created to allow the ship to tilt slightly when the left and right keys are pressed. The amount of tilt can be changed from the inspector once this code was placed in the playercontroller.

        
        rigidbody.rotation = Quaternion.Euler (0.0f0.0frigidbody.velocity.x * -tilt);
    }


Shooting



Because the bolt is dim and you can see the black background, I changed the shader to Mobile>Particles>Additive. This made the bolt more bright and glowing in colour and go rid of the black by making it transparent.


I formed my shot code within the playerController. The game object shot was created.

public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    
    private float nextFire;


Instantiate means an object to instantiate and a position and rotation to set.
The game object we will be attaching this to is the shotspawn, which I created as a child to the player.

Instantiate(shotshotSpawn.positionshotSpawn.rotation);


I positioned the shot spawn relative to the player ship, so it will always shoot out from the ship.



















The rest of the code allows when the button, in this case a mouseclick is pressed the shot will fire, which then after it will almost reset to allow the shot to be fired again. 

void Start()
    {
        shotSpawn = GameObject.Find ("Shot Spawn").GetComponent<Transform>();
    }

    void Update ()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shotshotSpawn.positionshotSpawn.rotation);
          
        }
    }

I also added a script to the bolt itself called Mover. This allows the bolt to move with the ship itself so the bolt always comes from the ship and not from one place on the scene.

using UnityEngine;
using System.Collections;

public class Mover : MonoBehaviour
{
    public float speed;
    
    void Start ()
    {
        rigidbody.velocity = transform.forward * speed;
    }
}


No comments: