Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 animeman0 · Jul 12, 2015 at 09:44 AM · c#errorraycastplayerenemy

enemy detect player then attack - c#

Hi,

In my game I want it so when the player enters the enemy raycast it goes towards him and attacks.

However I'm getting Error CS1503, the console says Assets/scripts/EnemyScript.cs(20,23): error CS1503: Argument #2' cannot convert object' expression to type UnityEngine.Vector3' and error CS1502 he best overloaded method match for UnityEngine.Debug.DrawLine(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Color)' has some invalid arguments

I also get error CS0019 it says Operator -' cannot be applied to operands of type UnityEngine.Vector3' and `float'

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
 
     bool seenPlayer = false;
     RaycastHit2D seePlayer;
 
     Animator anim;
 
 
     void Start()
     {
         anim = GetComponent<Animator> ();
     }
 
     void Update()
     {
         //Just a debug visual representation of the Linecast, can only see this in scene view! Doesn't actually do anything!
         Debug.DrawLine(transform.position, (transform.position - 10f), Color.magenta);
 
         //Using linecast which takes (start point, end point, layermask) so we can make it only detect objects with specified layers
         //its wrapped in an if statement, so that while the tip of the Linecast is touching an object with layer, the code inside executes
         if(Physics2D.Linecast(transform.position, (transform.position - 10f), 1 << LayerMask.NameToLayer("GameObject")))
         {
             //store the collider object the Linecast hit so that we can do something with that specific object
             //each time the linecast touches a new object with layer
             seePlayer = Physics2D.Linecast(transform.position, (transform.position - 10f), 1 << LayerMask.NameToLayer("Guard")); 
             seenPlayer = true; //since the linecase is touching the guard and we are in range, we can now interact!
         }
         else
         {
             seenPlayer = false; //if the linecast is not touching
         }
     }
 }

^ my script

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
1
Best Answer

Answer by EvilTak · Jul 12, 2015 at 09:49 AM

The problem in your code is wherever you have used (transform.position - 10f). transform.position is a Vector3 and 10f is a float. If say you want to go 10 units behind then you should use transform.position - transform.forward * 10. This way you can be sure it will always be 10 meters directly behind the transform. If forward then transform.position + transform.forward * 10. You can also replace 10 with a variable of your choice in the Inspector.

Comment
Add comment · Show 6 · 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 animeman0 · Jul 12, 2015 at 10:00 AM 0
Share

Thanks do you know what do after the enemy has detected the player. how would i make the enemy move towards the player

avatar image EvilTak · Jul 12, 2015 at 10:13 AM 0
Share

If you want to move the enemy towards the player you can use Nav$$anonymous$$eshAgent.SetDestination (if you are going to use Nav$$anonymous$$eshes, which is probably a good idea). Otherwise if the terrain or floor is completely flat and there aren't any obstacles in between you can use Rigidbody.$$anonymous$$ovePosition.

avatar image animeman0 · Jul 12, 2015 at 10:24 AM 0
Share

I tried what you said in your answer but now I've got a new error

Assets/scripts/EnemyScript.cs(22,84): error CS1061: Type UnityEngine.Transform' does not contain a definition for foward' and no extension method foward' of type UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)

avatar image animeman0 · Jul 12, 2015 at 10:49 AM 0
Share

you know what i'm actually an idiot. i spelt forward wrong

avatar image EvilTak · Jul 12, 2015 at 02:46 PM 0
Share

Yeah... typos are the worst enemy of a coder (unless if he uses IntelliSense :P) if the answer solved your problem, please accept it so that others may know also. If it didn't you can still tell me what the problem is...

Show more comments

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

22 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

Related Questions

Player Attack Script. I Need Help! 0 Answers

Help With Player Decrease Enemy Health!,Need Help with Character 1 Answer

Error CS1061 I can't find a solution! [C#] 1 Answer

How do I use a sphere collider to tell if my player is on the ground? 2 Answers

How can I check the line of sight to see if the player is visible to the enemy? 1 Answer


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