2 Issues with my SHUMP game
Hello people,
I'm making an SHUMP game after an tutorial that shows you how to do it and ran into 2 issues:
Playership
I've did the Scrolling script on it and put it on the player and turn on "Link to camera".
The Player drags down on the screen but moves as normal
But when it turns off, the player moves fine but pushes the enemy away, prevent you from firing at them or getting near them.
here is my PlayerMove script:
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
//1- The Speed of the ship
public Vector2 speed = new Vector2(50, 50);
//2-Store the movement
private Vector2 movement;
void Update ()
{
// 3- Retrieve axis information
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
//4- Movement per direction
movement = new Vector2(speed.x * inputX, speed.y * inputY);
//5- Shooting
bool shoot = Input.GetButtonDown("Fire1");
shoot |= Input.GetButtonDown("Fire2");
if (shoot)
{
Weapon weapon = GetComponent<Weapon>();
if (weapon != null)
{
//false because the player is not an enemy
weapon.Attack(false);
// Sound.Instance.MakePlayerShotSound(); }
}
// 6 - Make sure we are not outside the camera bounds
var dist = (transform.position - Camera.main.transform.position).z;
var leftBorder = Camera.main.ViewportToWorldPoint(
new Vector3(0, 0, dist)
).x;
var rightBorder = Camera.main.ViewportToWorldPoint(
new Vector3(1, 0, dist)
).x;
var topBorder = Camera.main.ViewportToWorldPoint(
new Vector3(0, 0, dist)
).y;
var bottomBorder = Camera.main.ViewportToWorldPoint(
new Vector3(0, 1, dist)
).y;
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
transform.position.z
);
// End of the update method
}
void FixedUpdate()
{
//move the game objects
GetComponent<Rigidbody2D>().velocity = movement;
}
void OnCollisionEnter2D(Collision2D collision)
{
bool damagePlayer = false;
// Collision with enemy
Enemy enemy = collision.gameObject.GetComponent<Enemy>();
if (enemy != null)
{
// Kill the enemy
Health enemyHealth = enemy.GetComponent<Health>();
if (enemyHealth != null) enemyHealth.Damage(enemyHealth.hp);
damagePlayer = true;
}
// Damage the player
if (damagePlayer)
{
Health playerHealth = this.GetComponent<Health>();
if (playerHealth != null) playerHealth.Damage(1);
}
}
}
Second Issue is the shot script, when i click fire1, The lazer would fire down sideways instead of firing straight like an arrow, pushing forward, not downwards.
This for Weapon script
using UnityEngine; using System.Collections;
public class Weapon : MonoBehaviour {
//Projectile Prefab for shooting
public Transform Greenlazer;
// Cooldown in seconds between two shots
public float shootingRate = 0.25f;
//-----------------------------
// 2 - Cooldown
//-----------------------------
private float shootCooldown;
void Start ()
{
shootCooldown = 0f;
}
// Update is called once per frame
void Update ()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
//------------------------
// 3 - Shooting from another script
//------------------------
public void Attack(bool isEnemy) {
if (CanAttack)
{
shootCooldown = shootingRate;
//Create a new shot
var shotTransform = Instantiate(Greenlazer) as Transform;
//Assign position
shotTransform.position = transform.position;
//The is enemy property
ProjectileShot shot = shotTransform.gameObject.GetComponent<ProjectileShot>();
if (shot != null)
{
shot.isEnemyShot = isEnemy;
}
//Make the weapon shot always toward it
MoveObjectScript move = shotTransform.gameObject.GetComponent<MoveObjectScript>();
if (move != null)
{
move.direction = this.transform.right;
}
}
}
public bool CanAttack
{
get
{
return shootCooldown <= 0f;
}
}
}
and the Move Object Script
using UnityEngine; using System.Collections;
public class MoveObjectScript : MonoBehaviour {
//Object Speed
public Vector2 speed = new Vector2(10, 10);
//Moving direction
public Vector2 direction = new Vector2(-1, 0);
private Vector2 movement;
void Update()
{
// 2- Movement
movement = new Vector2(speed.x * direction.x, speed.y * direction.y);
}
void FixedUpdate()
{
GetComponent<Rigidbody2D>().velocity = movement;
}
}
Anyone encounter issues with the ships being wobbly because the Playerscript and/or their lazer not firing foward instead it fires downards and sideaways, if so how you approach these issues?
Your answer
Follow this Question
Related Questions
Basic FPS help! 2 Answers
Projectiles not shooting correctly 1 Answer
How do i play animation when my player is moving 1 Answer
Coroutine Waits, But Calls Multiple Times per Update. Is There a Way to Fix This? 1 Answer
How could I add "gravity" to a top-down shooter's bullet, such as in The Binding of Isaac? 0 Answers