• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by LelandGMC · Nov 16, 2015 at 03:40 AM · programmingplayer movementprojectileshooter

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

  1. 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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

35 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Projectile with direction 0 Answers

Projectiles aren't shooting the direction they're supposed to with transform.up 0 Answers

How to make my snowball model to actually shoot like a projectile? 0 Answers

Arena Shooter Weapon W/ Pickups Scrolling by MouseWheel 0 Answers

Help with a roll effect 2D 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges