Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by OctoSloths · Dec 11, 2014 at 10:29 PM · 2daddforceshootingvector2enemyai

Enemy shoot at player in 2D game

I have a script I was intending to put on an empty game object in front of the enemy AI, however I am getting a lot of errors. These are the errors:

Assets/Meep/Scripts New/enemyshoottest.cs(16,71): error CS0019: Operator '*' cannot be applied to operands of type 'double' and 'UnityEngine.Vector3'

Assets/Meep/Scripts New/enemyshoottest.cs(16,26): error CS1502: The best overloaded method match for 'UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments

Assets/Meep/Scripts New/enemyshoottest.cs(16,26): error CS1503: Argument '#2' cannot convert 'object' expression to type 'UnityEngine.Vector3'

Assets/Meep/Scripts New/enemyshoottest.cs(20,52): error CS0119: Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected

Assets/Meep/Scripts New/enemyshoottest.cs(20,43): error CS1502: The best overloaded method match for 'UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2)' has some invalid arguments

Assets/Meep/Scripts New/enemyshoottest.cs(20,43): error CS1503: Argument '#1' cannot convert 'object' expression to type 'UnityEngine.Vector2'

Assets/Meep/Scripts New/enemyshoottest.cs(25,60): error CS0119: Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected

Assets/Meep/Scripts New/enemyshoottest.cs(25,43): error CS1502: The best overloaded method match for 'UnityEngine.Rigidbody2D.AddRelativeForce(UnityEngine.Vector2)' has some invalid arguments

Assets/Meep/Scripts New/enemyshoottest.cs(25,43): error CS1503: Argument '#1' cannot convert 'object' expression to type `UnityEngine.Vector2'

And here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class enemyshoottest : MonoBehaviour {
     public Rigidbody2D clone;
     public GameObject projectile;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         //attempt at bullet
         //Rigidbody2D : clone;
         clone = (Instantiate(projectile, transform.position+1.0*transform.forward,transform.rotation));
         // Debug.Log(clone.transform.position + " : " + transform.position);
         if(clone.transform.position.x < GameObject.FindWithTag("ariana").transform.position.x){
             clone.transform.eulerAngles = new Vector3(0,0,180);
             clone.rigidbody2D.AddForce(Vector2(-800,0));
         }
         else{
             // Give the cloned object an initial velocity along the current 
             // object's Z axis
             clone.rigidbody2D.AddRelativeForce(Vector2(800,0));
         }
     }
 }
 


Comment
Add comment · Show 1
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
avatar image Orr10c · Sep 14, 2016 at 12:31 PM 0
Share

but what object do you attach to clone, you can't attach an object from unity to a rigidbody var.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by OctoSloths · Dec 12, 2014 at 07:19 PM

I figured out the issue! Here is my new script for those with the same issue:

 using UnityEngine;
 using System.Collections;
 
 /// Launch projectile
 
 public class WeaponScript : MonoBehaviour
 {
 
     // 1 - Designer variables
 
     
 
     /// Projectile prefab for shooting
 
     public Rigidbody2D shotPrefab;
     
     /// <summary>
     /// Cooldown in seconds between two shots
     /// </summary>
     public float shootingRate = 0.25f;
     
 
     // 2 - Cooldown
     
     private float shootCooldown;
     
     void Start()
     {
         shootCooldown = 0f;
     }
     
     void Update()
     {
         if (shootCooldown > 0)
         {
             shootCooldown -= Time.deltaTime;
         }
     }
 
     public void Attack(bool isEnemy)
     {
         if (CanAttack)
         {
             shootCooldown = shootingRate;
             
             // Create a new shot
             //var shotTransform = Instantiate(shotPrefab) as Transform;
             
             // Assign position
             //shotTransform.position = transform.position;
         Rigidbody2D clone;
             clone = (Instantiate(shotPrefab, transform.position+1.0f*transform.forward,transform.rotation) as Rigidbody2D);
             //var rigidbody2D = Rigidbody2D;
             clone.rigidbody2D.AddRelativeForce(new Vector2 (1000, 0));
             //shotPrefab.Rigidbody2D.AddForce(transform.forward * 1000);
 
             // The is enemy property
             ShotScript shot = clone.gameObject.GetComponent<ShotScript>();
             if (shot != null)
             {
                 shot.isEnemyShot = isEnemy;
             }
             
             // Make the weapon shot always towards it
             MoveScript move = shotPrefab.gameObject.GetComponent<MoveScript>();
             if (move != null)
             {
                 //move.direction = this.transform.right; // towards in 2D space is the right of the sprite
             }
         }
     }
 
     public bool CanAttack
     {
         get
         {
             return shootCooldown <= 0f;
         }
     }
 }
Comment
Add comment · Show 2 · Share
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
avatar image CoolCasualCat · Jul 07, 2015 at 07:34 AM 0
Share

I'm very new to Unity and scripting, and certain words? (don't know to call them) are in red as in they are incorrect or unity doesn't recognize them. I have the exact same script copied and "AddRelativeForce, ShotScript, isEnemyShot, $$anonymous$$oveScipt" are all in red and are "not recoginzed".

avatar image MrSpuriz CoolCasualCat · Mar 08, 2016 at 01:35 AM 0
Share

it´s because these are scripts that he made and that he is refferencing to

avatar image
0

Answer by Ashcastillo007 · Dec 12, 2014 at 12:51 AM

I think that the problem is in the line 16... because transform.position and transform.forwards are vectors, and if you multiply a vector for vector you get a scalar number...

example: (1,2) * (3,4) = (1*3) + (2*4) = 11

you should find another method to write the position to get a vector result, like a vector sum

example: (1,2) + (3,4) = (4,6)

you must try this

 //This will instantiate a projectile 1 px in front 
 
 clone = (Instantiate(projectile, transform.position+Vector2(1,0),transform.rotation));

Comment
Add comment · Show 3 · Share
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
avatar image OctoSloths · Dec 12, 2014 at 01:11 AM 0
Share

I get the same errors as before besides the first three, the first three are now:

Assets/$$anonymous$$eep/Scripts New/enemyshoottest.cs(16,69): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

Assets/$$anonymous$$eep/Scripts New/enemyshoottest.cs(16,26): error CS1502: The best overloaded method match for 'UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments

Assets/$$anonymous$$eep/Scripts New/enemyshoottest.cs(16,26): error CS1503: Argument #2' cannot convert object' expression to type `UnityEngine.Vector3'

avatar image Ashcastillo007 · Dec 12, 2014 at 02:27 AM 0
Share

ok ok, mmm... try with this

 clone = (Instantiate(projectile, transform.position+Vector3(1,0,0),transform.rotation));
 
avatar image OctoSloths · Dec 12, 2014 at 03:09 PM 0
Share

I still get the same errors :( I think I'm just going to create a different script for enemy fire. Thank you for your help though!

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.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

My shooting script doesn't add force to the projectile 4 Answers

How to add force towards an object with variable degree accuracy 2d 1 Answer

2D TileMap: Determining which grid tiles a line passes over 0 Answers

Looking and firing towards mouse position, 2d game. 3 Answers

Why doesn't AddForce work upwards? 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