Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Why addforce isn't adding force on click?

$
0
0
Hi, I have this script attached to the enemy gameobject: using System.Collections; using System.Collections.Generic; using UnityEngine; public class AIFollow2 : MonoBehaviour { /*public GameObject player; private float speed = 10f; //bool canFollow = true; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { // if (canFollow) //{ //if (player.transform.position.x > 330 && player.transform.position.z < -30) //{ Quaternion targetRotation = Quaternion.LookRotation(player.transform.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 1 * Time.deltaTime); transform.position += transform.forward * speed * Time.deltaTime; //} //} } /* void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "gate") { // canFollow = false; // gameObject.SetActive(false); //Destroy(gameObject); speed = 0f; } }*/ //global variables private Transform target; private float speed = 2f; private Vector3 speedRot = Vector3.right * 50f; private FlashImage flashImage = null; public float rangeDist; bool attacking; public float moveForce; void Start () { target = GameObject.FindGameObjectWithTag("player").transform; // initializing stuff flashImage = GameObject.Find("FlashImage").GetComponent(); } void Update () { transform.Rotate (speedRot * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime); // move toward player float distance = Vector3.Distance(target.transform.position, gameObject.transform.position); if (distance <= rangeDist && !attacking){ // if in range, attack attacking = true; InvokeRepeating("AttackPlayer",3.0f,2.0f); } else if(distance > rangeDist && attacking ) { attacking = false; CancelInvoke("AttackPlayer"); } } void OnMouseDown(){ // NOT WORKING gameObject.GetComponent().AddForce((Camera.main.ScreenToWorldPoint(Input.mousePosition) - gameObject.transform.position).normalized * moveForce, ForceMode.Impulse); } void AttackPlayer(){ ScoringSystem.theScore -= 5; flashImage.StartFlash(.5f,0.5f,Color.red); //flash screen red } } When I run this, and click on enemy, I get this error:>> NullReferenceException: Object reference not set to an instance of an> object AIFollow2.OnMouseDown () (at> Assets/Scripts/AIFollow2.cs:75)> UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32) I'm not sure why this is happening and how to fix it, can someone please help? thanks.

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>