- Home /
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
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.
Thanks do you know what do after the enemy has detected the player. how would i make the enemy move towards the player
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.
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?)
you know what i'm actually an idiot. i spelt forward wrong
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...
Your answer
![](https://koobas.hobune.stream/wayback/20220611230934im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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