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

projectile.velocity error.

Discussion in 'Scripting' started by liggibernardo05, Jun 4, 2022.

  1. liggibernardo05

    liggibernardo05

    Joined:
    Apr 27, 2022
    Posts:
    21
    I found this script in C but it doesn't work and I was unable to debug. Can anyone correct my mistakes please, thank you very much.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shooting : MonoBehaviour {
    5.  
    6. public Rigidbody bulletPrefab;
    7.       public float shootSpeed = 300;
    8.      
    9.       private bool playerInRange = false;
    10.       private float lastAttackTime = 0f;
    11.       private float fireRate = 0.5f; //how many bullets are fired/second
    12.       private Transform player = null;
    13.      
    14.       void OnTriggerEnter(Collider other)
    15.       {
    16.           if(other.tag == "enemy")
    17.           {
    18.               playerInRange = true;
    19.               player = other.transform;
    20.           }
    21.       }
    22.      
    23.       void OnTriggerExit(Collider other)
    24.       {
    25.           if(other.tag == "enemy")
    26.           {
    27.               playerInRange = false;
    28.               player = null;
    29.           }
    30.       }
    31.      
    32.       void Update()
    33.       {
    34.           if (playerInRange)
    35.           {
    36.               //Rotate the enemy towards the player
    37.               transform.rotation = Quaternion.LookRotation(player.position - transform.position, transform.up);
    38.              
    39.               if (Time.time - lastAttackTime >= 1f/fireRate)
    40.               {
    41.                   shootBullet();
    42.                   lastAttackTime = Time.time;
    43.               }
    44.           }
    45.       }
    46.      
    47.       void shootBullet()
    48.       {
    49.        
    50.           var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);
    51.           //Shoot the Bullet in the forward direction of the player
    52.           projectile.velocity = transform.forward * shootSpeed;
    53.     }
    54. }
     
  2. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    In a word: no. You haven't said what doesn't work. Does it compile? Do the trigger enter and exit functions get called? Does playerInRange ever get set true? Does the bullet shoot? Does it go in the right direction?

    Assuming it compiles, add in lots of debug logging to see what does and doesn't work.
     
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Your problem is with the instantiate line at the bottom, https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    Unless you know what your doing never just copy & paste code. You wont learn anything & if it breaks you will have no idea how to fix it since you dont know how it works. Its good practice to write it out yourself compile as you go & deal with the errors as they crop up.
     
    MedalHellWay likes this.