Player Respawn Problem, Bullets dont move! [C#]

Updated on May 5, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on May 5, 2017

Hi! I finished the codes from video 12, when i noticed a problem, everytime my player respawns, the bullets dont move, they appear in position 0, 0, 0. I was receiving some errors, so i tried to use the FindPlayer like in the video but with some modifications, and it worked, no more errors! But, the bullets still spawn in position 0,0,0 and do not move, what im doing wrong? Also Sorry for the bad english. Here’s the code im using:

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets._2D;
 public class MoveTrail : MonoBehaviour
 {
 public float maxSpeed = 40f;
 PlatformerCharacter2D platformerCharacter2D;
 Vector3 pos;
 Vector3 velocity;
 GameObject player;
 float nextTimeToSearch = 0;
 //---------------------------------------------------------------------------------------------------
 void Awake()
 {
 GameObject player = GameObject.Find("Player");
 if (player == null)
 {
 FindPlayer();
 return;
 }
 platformerCharacter2D = player.GetComponent();
 velocity = new Vector3(maxSpeed * Time.deltaTime, 0, 0);
 pos = transform.position;
 }
 //---------------------------------------------------------------------------------------------------
 void FindPlayer()
 {
 if (nextTimeToSearch <= Time.time)
 {
 GameObject searchResult = GameObject.FindGameObjectWithTag("Player");
 if (searchResult != null)
 {
 player = searchResult;
 nextTimeToSearch = Time.time + 0.5f;
 }
 }
 }
 void FindComponent()
 {
 if (nextTimeToSearch <= Time.time)
 {
 GameObject searchResult = GameObject.FindGameObjectWithTag("Player");
 if (searchResult != null)
 {
 platformerCharacter2D = searchResult.GetComponent();
 nextTimeToSearch = Time.time + 0.5f;
 }
 }
 }
 void Start()
 {
 if (platformerCharacter2D == null)
 {
 FindComponent();
 return;
 }
 if (platformerCharacter2D.m_FacingRight == true)
 {
 velocity = new Vector3(maxSpeed * 1 * Time.deltaTime, 0, 0);
 }
 else
 {
 velocity = new Vector3(maxSpeed * -1 * Time.deltaTime, 0, 0);
 }
 }
 //---------------------------------------------------------------------------------------------------
 void Update()
 {
 pos += velocity;
 transform.position = pos;
 Destroy(gameObject, 1);
 }
 }
 
  • Liked by
Reply