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 Movement Script PROBLEM! Help!

Discussion in 'Scripting' started by antibiotikas, May 14, 2015.

  1. antibiotikas

    antibiotikas

    Joined:
    May 14, 2015
    Posts:
    5
    Hello guys! I've encountered a problem I can't solve yet, which shouldn't be hard to work out for you I guess.
    Quick review:

    1) Game is like Tower Defence.
    2) Towers have to spin around to face objects, which have their own path to go till the end of the map.
    3) Towers shoot the object, which is the closest to them;
    4) Instantiated projectile just appears on the spawn point and directly teleports to the object (bubble's) position.
    The problem is that I don't want the projectile immediately teleport to the place It's supposed to go. Is it a way to move the projectiles along a ray (slowly, at least noticeably)?
    5) Projectile is created with the line renderer.

    What I was doing before in the projectile script :

    public float SP;

    Void Update(){

    transform.position = Vector3.Lerp (transform.position, bubble.transform.position, SP * Time.deltaTime);
    Destroy(this.gameObject, 0.5f);

    }

    I'd really appreciate the help from you guys! It's really embarrassing and sad that I can't solve it myself for quite a long time... :(

    Thanks!
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    don't use vector3.lerp...

    try Vector3.MoveTowards
     
  3. antibiotikas

    antibiotikas

    Joined:
    May 14, 2015
    Posts:
    5
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletTrail : MonoBehaviour {
    5.     GameObject bubble;
    6.     void Start(){
    7.         bubble = GetComponent<FirstTower> ().closeBubble;
    8.     }
    9.  
    10.     void Update () {
    11.         if (bubble != null)
    12.         transform.position = Vector3.MoveTowards (transform.position, bubble.transform.position, 1000);
    13.         Destroy (this.gameObject,1f);
    14.     }
    15. }
    16.  
    Bad thing - my closest bubble changes pretty fast, since they keep spawning and my projectile stops and destroys itself on the instantiated place, so it does not even move to the bubble now.

    Also (probably because of the constantly changing close bubble) getting error :
    NullReferenceException: Object reference not set to an instance of an object
    BulletTrail.Start () (at Assets/BulletTrail.cs:7)
     
  4. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    I wouldnt use get component like that. Use get component once to get the component. Then down in update use FirstTower. CloseBubble. Or you are just trying to constantly access that first bubble from when you referenced the gameobject?

    I dont know what the close bubble script is doing though.

    Halbera
     
  5. antibiotikas

    antibiotikas

    Joined:
    May 14, 2015
    Posts:
    5
    Since it is array of many different bubbles, ye - it has to be in Update just noticed. The script FirstTower basically constantly detects/finds the closest object (closeBubble in this case) to the tower. I could use closeBubble on other scripts before, but now it can't work on my cloned projectile. Weird.
     
  6. antibiotikas

    antibiotikas

    Joined:
    May 14, 2015
    Posts:
    5
    When I use Vector3.MoveTowards, the projectile teleports on the gameobject, but not travels. It would be good if I could use a ray and move a projectile along it

    EDIT: Found a solution :

    Code (CSharp):
    1.     void Projectiles() {
    2.     Transform bulletClone = (Transform)Instantiate (BulletTrailPrefab, ShootingPlace.position, ShootingPlace.rotation);
    3.     Vector3 direction = (closeBubble.transform.position - transform.position) * 10;
    4.     if (closeBubble != null)
    5.         bulletClone.GetComponent<Rigidbody2D> ().velocity = direction;
    6.     }
    Now It's smooth projectile moving to the closebubble :) Hope this helps somebody later.
     
    Last edited: May 15, 2015