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 09, 2014 at 08:27 PM · projectileenemyai

Enemy shooting at player Unity2D

I'm trying to make it so that my enemy AI can shoot at and target the player in a 2D game. I get the errors:

Assets/Meep/Dialogues/enemyshoot.cs(21,35): error CS0118: enemyshoot.Projectile' is a field' but a 'type' was expected

Assets/Meep/Dialogues/enemyshoot.cs(22,28): error CS1061: Type object' does not contain a definition for instantiate' and no extension method instantiate' of type object' could be found (are you missing a using directive or an assembly reference?)

This is my current script:

 using UnityEngine;
 using System.Collections;
 
 public class enemyshoot : MonoBehaviour {
     public GameObject _controller;
     private Vector2 _direction;
     private Vector2 _startPosition;
     private float _canFireIn;
     public GameObject Projectile;
     public void Start (){
         //_controller = GetComponent<>();
         _direction = new Vector2(-1, 0);
         _startPosition = transform.position;
     }
     public void Update() {
         if ((_canFireIn -= Time.deltaTime) > 0)
             return;
         var raycast = Physics2D.Raycast(transform.position, _direction, 10, 1 << LayerMask.NameToLayer("Player"));
         if (!raycast)
             return;
         var projectile = (Projectile) Instantiate(Projectile, transform.position, transform.rotation);
         projectile.instantiate(gameObject, _direction, _controller.Velocity);
         //_canFireIn = FireRate;
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MolochSim · Dec 09, 2014 at 08:43 PM

The problem is that you have a class called Projectile and therefore cannot declare a variable with the name Projectile. Simply changing that to projectile wiwthout the capital P will fix the first error. In line 21 and 9.

I can't tell if you have an instantiate method iside the Projectile class. But from the error, you seem not to have one. What are you trying to achieve in line 22?

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 OctoSloths · Dec 09, 2014 at 09:15 PM 0
Share

I'm trying to make it so that the projectile appears and is launched towards the player, however I'm not entirely sure how to go about doing that.

avatar image MolochSim · Dec 09, 2014 at 09:58 PM 0
Share

First do what I said in my answer. I assume you are using a prefab as it is the most common way of approaching projectiles. If so, delete line 22. Add a movement script to the prefab, that will make it advance in its trajectory, or track the target you set for it. The movement should probably reside within a script attached to the projectile prefab in the Update function.

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

27 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

Related Questions

projectile formula 1 Answer

Calculate parabolic initial velocity and angle? 0 Answers

Enemy shooting player driving me crazy! 2 Answers

How do I apply damage to the enemy ai with projectile 0 Answers

Move projectile in a specific direction 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