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

Help With projectile

Discussion in 'Scripting' started by BulawOw, Jun 24, 2019.

  1. BulawOw

    BulawOw

    Joined:
    Nov 14, 2017
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Projectile : MonoBehaviour
    6. {
    7.  
    8.     Player player;
    9.  
    10.     public float projectileSpeed;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         player = FindObjectOfType<Player>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (player.turnedRight == true)
    21.         {
    22.             transform.Translate(Vector3.right * projectileSpeed * Time.deltaTime);
    23.         }
    24.         else if (player.turnedRight == false)
    25.         {
    26.             transform.Translate(Vector3.left * projectileSpeed * Time.deltaTime);
    27.         }
    28.     }
    29. }
    30.  
    So its my projectile code, and i have problem cause when i turn my character by pressing a or d when projectile is spawned the projectile also changing the direction that is going but i dont want that i want when projectile is spawned when character is turned right the projectile go right no matter what but i dont rly have any idea how to make that any tips ?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Move your turnedRight check to Start and set a class variable to left or right, then use that variable in Update function to move your projectile.
     
    BulawOw likes this.
  3. BulawOw

    BulawOw

    Joined:
    Nov 14, 2017
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Projectile : MonoBehaviour
    6. {
    7.  
    8.     public Player player;
    9.     public bool travelRight;
    10.     public bool travelLeft;
    11.  
    12.     public float projectileSpeed;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         player = FindObjectOfType<Player>();
    17.  
    18.         if (player.turnedRight == true)
    19.         {
    20.             travelRight = true;
    21.         }
    22.         else if (player.turnedRight == false)
    23.         {
    24.             travelLeft = true;
    25.         }
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         if (travelRight == true)
    32.         {
    33.             TravelRight();
    34.         }
    35.         else if (travelLeft == true)
    36.         {
    37.             TravelLeft();
    38.         }
    39.     }
    40.  
    41.     void TravelLeft()
    42.     {
    43.         transform.Translate(Vector3.left * projectileSpeed * Time.deltaTime);
    44.     }
    45.     void TravelRight()
    46.     {
    47.         transform.Translate(Vector3.right * projectileSpeed * Time.deltaTime);
    48.     }
    49.  
    50.    
    51. }
    52.  
    Its working while projectile is spawned its not changing directions but there is a little problem even when my character is turned left my projectiles going right all the time i tried do figured it out my self but im kinda stuck the right bools getting checked but its always calling function TravelRight()
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Try to make variables private.
     
  5. BulawOw

    BulawOw

    Joined:
    Nov 14, 2017
    Posts:
    3
    Still the same problem
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Let's simplify your code a little and add some log

    Code (CSharp):
    1.     private Vector3 direction;  
    2.  
    3.     void Start()
    4.     {
    5.         player = FindObjectOfType<Player>();
    6.         Debug.LogFormat("Is player turned right? {0}", player.turnedRight);
    7.         if (player.turnedRight)
    8.         {
    9.             direction = Vector3.right;
    10.         }
    11.         else
    12.         {
    13.             direction = Vector3.left;
    14.         }
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         transform.Translate(direction * projectileSpeed * Time.deltaTime);
    20.     }
    What it prints now?