Question by
ToukaKirishima1987 · Mar 08, 2018 at 04:55 PM ·
scripting problemgamegameobjects
How do I make my player object shoot shots on mobile?
Okay I am making a space shooter type of game but for mobile, so I have been trying to get my player object to shoot shots correctly.
I was able to shoot shots but they didn't stay with the player object, they would stay and shoot at the same spot. But now my shots creates invisible shots or the shots spawn way above the camera view. They do not stay with the player object and I also use a button to fire my shots.
This is the coding
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Shooter : MonoBehaviour {
Rigidbody2D rb;
float dirX;
[SerializeField]
float moveSpeed = 5f, jumpForce = 600f, bulletSpeed = 500f;
bool facingRight = true;
Vector3 localScale;
public Rigidbody2D ShotSpawn;
// Use this for initialization
void Start () {
localScale = transform.localScale;
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
dirX = CrossPlatformInputManager.GetAxis ("Horizontal");
if (CrossPlatformInputManager.GetButtonDown ("Jump"))
Jump ();
if (CrossPlatformInputManager.GetButtonDown ("Fire1"))
Fire ();
}
void FixedUpdate()
{
rb.velocity = new Vector2 (dirX * moveSpeed, rb.velocity.y);
}
void LateUpdate()
{
CheckWhereToFace ();
}
void CheckWhereToFace()
{
if (dirX > 0)
facingRight = true;
else
if (dirX < 0)
facingRight = false;
if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
localScale.x *= -1;
transform.localScale = localScale;
}
void Jump()
{
if (rb.velocity.y == 0)
rb.AddForce (Vector2.up * jumpForce);
}
void Fire()
{
var firedBullet = Instantiate(ShotSpawn);
}
}
I would like some help to figure out on to fix the problem of shots. Thank you for taking your time reading this.
Comment