- Home /
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));
}
}
}
but what object do you attach to clone, you can't attach an object from unity to a rigidbody var.
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;
}
}
}
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".
it´s because these are scripts that he made and that he is refferencing to
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));
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'
ok ok, mmm... try with this
clone = (Instantiate(projectile, transform.position+Vector3(1,0,0),transform.rotation));
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
Follow this Question
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