Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why addforce isn't adding force on click?

Discussion in 'Input System' started by coder_58, Jul 17, 2021.

  1. coder_58

    coder_58

    Joined:
    Mar 29, 2020
    Posts:
    31
    Hi,

    I have this script attached to the enemy gameobject:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AIFollow2 : MonoBehaviour
    6. {
    7.     /*public GameObject player;
    8.     private float speed = 10f;
    9.     //bool canFollow = true;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.       //  if (canFollow)
    20.        //{
    21.         //if (player.transform.position.x > 330 && player.transform.position.z < -30)
    22.         //{
    23.         Quaternion targetRotation = Quaternion.LookRotation(player.transform.position - transform.position);
    24.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 1 * Time.deltaTime);
    25.         transform.position += transform.forward * speed * Time.deltaTime;
    26.         //}
    27.         //}
    28.     }
    29. /*
    30.     void OnTriggerEnter(Collider other)
    31.     {
    32.         if (other.gameObject.tag == "gate")
    33.         {
    34.            // canFollow = false;
    35.             // gameObject.SetActive(false);
    36.             //Destroy(gameObject);
    37.             speed = 0f;
    38.         }
    39.     }*/
    40.  
    41.     //global variables
    42.      private Transform target;
    43.     private float speed = 2f;
    44.     private Vector3 speedRot = Vector3.right * 50f;
    45.     private FlashImage flashImage = null;
    46.     public float rangeDist;
    47.     bool attacking;
    48.    
    49.     public float moveForce;
    50.     void Start () {
    51.         target = GameObject.FindGameObjectWithTag("player").transform; // initializing stuff
    52.         flashImage = GameObject.Find("FlashImage").GetComponent<FlashImage>();
    53.     }
    54.    
    55.     void Update () {
    56.         transform.Rotate (speedRot * Time.deltaTime);
    57.         transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime); // move toward player
    58.  
    59.         float distance = Vector3.Distance(target.transform.position, gameObject.transform.position);
    60.      
    61.    
    62.         if (distance <= rangeDist && !attacking){ // if in range, attack
    63.  
    64.             attacking = true;
    65.             InvokeRepeating("AttackPlayer",3.0f,2.0f);
    66.         }
    67.         else if(distance > rangeDist && attacking )
    68.         {
    69.             attacking = false;
    70.             CancelInvoke("AttackPlayer");
    71.         }
    72.     }
    73.  
    74.     void OnMouseDown(){ // NOT WORKING
    75.         gameObject.GetComponent<Rigidbody>().AddForce((Camera.main.ScreenToWorldPoint(Input.mousePosition) - gameObject.transform.position).normalized * moveForce, ForceMode.Impulse);
    76.     }
    77.  
    78.     void AttackPlayer(){
    79.         ScoringSystem.theScore -= 5;
    80.         flashImage.StartFlash(.5f,0.5f,Color.red); //flash screen red
    81.     }
    82.  
    83. }
    84.  
    85.  
    86.  
    87.  
    88.  
    When I run this, and click on enemy, I get this error:
    I'm not sure why this is happening and how to fix it, can someone please help? thanks.
     
  2. KomeijiSatorin

    KomeijiSatorin

    Joined:
    Jun 13, 2021
    Posts:
    31
    You did not specify if you are using the new input system... If you are using it, then I suppose you are using the Send Messages behavior. In this case, what the playerInput will call, is the function named On[Name of the Action](), by example if it is "LeftClick" then OnLeftClick() is called. Also, OnMouseDown is predefined by unity so avoid using it as action name. FYI: about OnMouseDown https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
    So if you are not using the new input system, you have to check if you have a collider on the object.