Search Unity

Simple issue, but overthinking

Discussion in 'Scripting' started by Phobiegames, Jan 21, 2019.

  1. Phobiegames

    Phobiegames

    Joined:
    Apr 3, 2017
    Posts:
    113
    Currently I have a top down cube that moves around with basic tank controls. I'm trying to get the shooting down and I know i have multiple ways of attempting to do so, such as using a rigidbody to add force forward or using velocity or even just a transform. I'm leaning more transform since this game is gonna be for mobile and i just personally want to keep it simple. Right now i have two scripts, one is the player controller that has the shooting input, and I have the projectile which just simple destroys itself after an amount of time. I have the prefabs spawning on the emit transform the way i want, however when i try to add a force or a transform it only shoots off in one direction and not in the direction the emit object is facing (which is parented to the player)

    here is the player controller script I have at the moment without the movement:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     [Header("Movement Parameters")]
    8.     public float Speed = 3.0F;
    9.     public float RotateSpeed = 3.0F;
    10.  
    11.     [Header("Shooting Parameters")]
    12.     public float shootSpeed = 2f;
    13.     public Transform emitBullets;
    14.     public GameObject bulletToShoot;
    15.  
    16.     void Update()
    17.     {
    18.         PlayerMovement();
    19.  
    20.         if (Input.GetButton("Fire1"))
    21.         {
    22.             Shoot();
    23.         }
    24.     }
    25.  
    26.     public void PlayerMovement()
    27.     {
    28.         CharacterController controller = GetComponent<CharacterController>();
    29.         if (transform != null)
    30.         {
    31.             transform.Rotate(0, Input.GetAxis("Horizontal") * RotateSpeed, 0);
    32.             var forward = transform.TransformDirection(Vector3.forward);
    33.             float curSpeed = Speed * Input.GetAxis("Vertical");
    34.             controller.SimpleMove(forward * curSpeed);
    35.         }
    36.     }
    37.  
    38.     public void Shoot()
    39.     {
    40.         GameObject bullet = Instantiate(bulletToShoot, emitBullets.transform.position, Quaternion.identity) as GameObject;
    41.     }
    42. }
     
  2. Phobiegames

    Phobiegames

    Joined:
    Apr 3, 2017
    Posts:
    113
    so just a quick update but not quite a solution, I added this to my projectile script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Projectile : MonoBehaviour {
    6.  
    7.     [Header("Despawn Timer")]
    8.     public float deathTime = 2f;
    9.  
    10.     private void Update()
    11.     {
    12.         transform.Translate(transform.forward * 10 * Time.smoothDeltaTime);
    13.         Destroy(gameObject, deathTime);
    14.     }
    15. }
    after doing this, it moves it forward, then inside my shoot method on the playercontroller script i added this:

    Code (CSharp):
    1.     public void Shoot()
    2.     {
    3.         GameObject bullet = Instantiate(bulletToShoot, emitBullets.position, emitBullets.rotation) as GameObject;
    4.     }
    which gets it pretty close to what im looking for, however when the player is shooting the in opposite direction it tries to shoot upwards again instead of down. So basically i'm closer but not there
     
  3. imasmou

    imasmou

    Joined:
    Dec 5, 2017
    Posts:
    3
    Top Down Cube?
    and it is shooting in the wrong direction?

    i am not sure what you are trying to explain sir but still
    try and check if your player is flipped
    thats all i can tell by seeing what you have said so far.
     
  4. Phobiegames

    Phobiegames

    Joined:
    Apr 3, 2017
    Posts:
    113
    yeah its just a character(but its a cube) and the camera is in a top down view. When i shoot the bullets they basically aren't going in the direction the empty game object i have parented to the character is facing. its upright lol sorry for the confusion. I just meant the camera is viewing the player from a top down view.
     
    Last edited: Jan 21, 2019