Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[FIXED]Simple Error bullets not moving forward with add force

Discussion in 'Scripting' started by longarm123, Sep 28, 2016.

  1. longarm123

    longarm123

    Joined:
    Mar 9, 2013
    Posts:
    8
    I checked the collider off and even added an enter collision in case it is crashing the player, but it isnt. I feel like this is a simple mistake

    Spawn Bullet script
    Code (CSharp):
    1.     void Update () {
    2.         if (Input.GetMouseButtonDown(0))
    3.         {
    4.             Vector3 pos = Input.mousePosition;
    5.             pos.z = transform.position.z - Camera.main.transform.position.z;
    6.             pos = Camera.main.ScreenToWorldPoint(pos);
    7.  
    8.             Quaternion q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
    9.             go = (GameObject)Instantiate(prefab, transform.position, q);        }
    10.     }
    bullet prefab script
    Code (CSharp):
    1.     void awake()
    2.     {
    3.         this.GetComponent<Rigidbody>().AddForce(transform.forward * 500f);
    4.  
    5.     }
    6.  
    7.     void OnCollisionEnter()
    8.     {
    9.         Debug.Log("hello");
    10.     }
    11.  
    Thanks,
     
  2. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    It's because you wrote
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    Now it only moves forward for one frame, you want to use
    Code (CSharp):
    1. if (Input.GetMouseButton(0))
     
  3. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Try "void Awake()" instead of "void awake()" (upper case A).
     
  4. longarm123

    longarm123

    Joined:
    Mar 9, 2013
    Posts:
    8
    After the Awake fix, and mouse button down, the error RigidBody doesn't exist which i fixed because its RigidBody2D. Now no error shows up, and bullet still doesn't move

    Fixed, transform.forward doesn't work on 2D. Since that is toward teh camera. forgot about that. My mistake, thanks for the help
     
    Last edited: Sep 28, 2016