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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

My particles keep falling!

Discussion in 'Scripting' started by Alexandress, Jun 4, 2018.

  1. Alexandress

    Alexandress

    Joined:
    May 28, 2018
    Posts:
    5
    Hello!

    I have a really annoying problem with my foot dust mario-like particle. They keep falling down for no reason. I have set a cube at the feet of my character and instantiate the effect each time the player move and every 0.25sc. However overtime it seems the effects are spawning under my character (it looks like the Y axis is dropping down for no reason)
    But my cube has no gravity and has a fixed position and so does my particles! I'm becoming crazy! I'm quite a beginner so sorry if the code is a mess. Any help is welcome!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float moveSpeed;
    7.     //public Rigidbody theRB;
    8.     public float jumpForce;
    9.     public CharacterController controller;
    10.     private Vector3 moveDirection;
    11.     public float gravityScale;
    12.     public Animator anim;
    13.     public Transform pivot;
    14.     public float rotateSpeed;
    15.     public GameObject playerModel;
    16.     private int jumpNumber = 0;
    17.     public GameObject footEffect;
    18.     private bool footBool = false;
    19.     private bool footBool2 = false;
    20.     public Transform footCube;
    21.     public float knobackForce;
    22.     public float knobackTime;
    23.     private float knobackCounter;
    24.     // Use this for initialization
    25.     void Start()
    26.     {
    27.         //theRB = GetComponent<Rigidbody>();
    28.         controller = GetComponent<CharacterController>();
    29.     }
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         //theRB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, theRB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
    34.         /*if (Input.GetButtonDown("Jump"))
    35.         {
    36.             //theRB.velocity = new Vector3(theRB.velocity.x, jumpForce, theRB.velocity.z);
    37.         }*/
    38.         //moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
    39.         if (footBool == false && footBool2 == false)
    40.         {
    41.             StartCoroutine(footBoolReset());
    42.             footBool2 = true;
    43.         }
    44.         if (knobackCounter <= 0)
    45.         {
    46.             float yStore = moveDirection.y;
    47.             moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    48.             moveDirection = moveDirection.normalized * moveSpeed;
    49.             moveDirection.y = yStore;
    50.            if (controller.isGrounded || jumpNumber < 1)
    51.             {
    52.                 if (Input.GetButtonDown("Jump"))
    53.                 {
    54.                     moveDirection.y = jumpForce;
    55.                     jumpNumber += 1;
    56.                 }
    57.             }
    58.         }else
    59.         {
    60.             knobackCounter -= Time.deltaTime;
    61.         }
    62.         if (!controller.isGrounded)
    63.         {
    64.             moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    65.         }
    66.         if (controller.isGrounded)
    67.         {
    68.             jumpNumber = 0;
    69.         }
    70.         if (controller.isGrounded)
    71.         {
    72.             if (footBool == true && (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
    73.             {
    74.                 Instantiate(footEffect, footCube.position, Quaternion.identity);
    75.                 footBool = false;
    76.             }
    77.         }
    78.         controller.Move(moveDirection * Time.deltaTime);
    79.         //Move the player in differents direction based on camera look direction
    80.         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
    81.         {
    82.             transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
    83.             Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0, moveDirection.z));
    84.             playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
    85.         }
    86.         anim.SetBool("isGrounded", controller.isGrounded);
    87.         anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
    88.         if (jumpNumber == 1)
    89.         {
    90.             anim.SetInteger("Double", 1);
    91.         }
    92.         if (jumpNumber == 0)
    93.         {
    94.             anim.SetInteger("Double", 0);
    95.         }
    96.     }
    97.     IEnumerator footBoolReset()
    98.     {
    99.         if (footBool == false)
    100.         {
    101.             yield return new WaitForSeconds(0.25F);
    102.             footBool = true;
    103.             footBool2 = false;
    104.         }
    105.     }
    106.     public void Knoback(Vector3 direction)
    107.     {
    108.         knobackCounter = knobackTime;
    109.         moveDirection = direction * knobackForce;
    110.         moveDirection.y = knobackForce;
    111.     }
    112. }
    Video of the problem (and trying a fix from the Discord with localPosition)

    When I remove my delay of 0.25 s between each particle spawn (you can some randomly spawn on the y axis...)

     
    Last edited: Jun 5, 2018
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,113
    Please include the relevant code in Code tags. The resolution of your video is so low that it's very difficult to read any of that code.
     
    Alexandress likes this.
  3. Alexandress

    Alexandress

    Joined:
    May 28, 2018
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float moveSpeed;
    7.     //public Rigidbody theRB;
    8.     public float jumpForce;
    9.     public CharacterController controller;
    10.     private Vector3 moveDirection;
    11.     public float gravityScale;
    12.     public Animator anim;
    13.     public Transform pivot;
    14.     public float rotateSpeed;
    15.     public GameObject playerModel;
    16.     private int jumpNumber = 0;
    17.     public GameObject footEffect;
    18.     private bool footBool = false;
    19.     private bool footBool2 = false;
    20.     public Transform footCube;
    21.     public float knobackForce;
    22.     public float knobackTime;
    23.     private float knobackCounter;
    24.     // Use this for initialization
    25.     void Start()
    26.     {
    27.         //theRB = GetComponent<Rigidbody>();
    28.         controller = GetComponent<CharacterController>();
    29.     }
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         //theRB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, theRB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
    34.         /*if (Input.GetButtonDown("Jump"))
    35.         {
    36.             //theRB.velocity = new Vector3(theRB.velocity.x, jumpForce, theRB.velocity.z);
    37.         }*/
    38.         //moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
    39.         if (footBool == false && footBool2 == false)
    40.         {
    41.             StartCoroutine(footBoolReset());
    42.             footBool2 = true;
    43.         }
    44.         if (knobackCounter <= 0)
    45.         {
    46.             float yStore = moveDirection.y;
    47.             moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    48.             moveDirection = moveDirection.normalized * moveSpeed;
    49.             moveDirection.y = yStore;
    50.            if (controller.isGrounded || jumpNumber < 1)
    51.             {
    52.                 if (Input.GetButtonDown("Jump"))
    53.                 {
    54.                     moveDirection.y = jumpForce;
    55.                     jumpNumber += 1;
    56.                 }
    57.             }
    58.         }else
    59.         {
    60.             knobackCounter -= Time.deltaTime;
    61.         }
    62.         if (!controller.isGrounded)
    63.         {
    64.             moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    65.         }
    66.         if (controller.isGrounded)
    67.         {
    68.             jumpNumber = 0;
    69.         }
    70.         if (controller.isGrounded)
    71.         {
    72.             if (footBool == true && (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
    73.             {
    74.                 Instantiate(footEffect, footCube.position, Quaternion.identity);
    75.                 footBool = false;
    76.             }
    77.         }
    78.         controller.Move(moveDirection * Time.deltaTime);
    79.         //Move the player in differents direction based on camera look direction
    80.         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
    81.         {
    82.             transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
    83.             Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0, moveDirection.z));
    84.             playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
    85.         }
    86.         anim.SetBool("isGrounded", controller.isGrounded);
    87.         anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
    88.         if (jumpNumber == 1)
    89.         {
    90.             anim.SetInteger("Double", 1);
    91.         }
    92.         if (jumpNumber == 0)
    93.         {
    94.             anim.SetInteger("Double", 0);
    95.         }
    96.     }
    97.     IEnumerator footBoolReset()
    98.     {
    99.         if (footBool == false)
    100.         {
    101.             yield return new WaitForSeconds(0.25F);
    102.             footBool = true;
    103.             footBool2 = false;
    104.         }
    105.     }
    106.     public void Knoback(Vector3 direction)
    107.     {
    108.         knobackCounter = knobackTime;
    109.         moveDirection = direction * knobackForce;
    110.         moveDirection.y = knobackForce;
    111.     }
    112. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    I don't see any code in the above snippet that interacts with particles to turn them on or off. Did I just miss it? Where is that function handled?
     
    Alexandress likes this.
  5. Alexandress

    Alexandress

    Joined:
    May 28, 2018
    Posts:
    5
    I "turn them on" with that (Condition is if the character is grounded and move since I don't want any foot dust thing if he is idle) :
    Code (CSharp):
    1.         if (controller.isGrounded)
    2.         {
    3.             if (footBool == true && (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
    4.             {
    5.                 Instantiate(footEffect, footCube.position, Quaternion.identity);
    6.                 footBool = false;
    7.             }
    8.         }
    My particles are destroyed with another script called Destroy :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Destroy : MonoBehaviour {
    6.  
    7.     public float lifetime;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.  
    17.         Destroy(gameObject, lifetime);
    18.  
    19.    
    20.     }
    21. }
    22.  
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    In line 5 above (top script) you instantiate the gameobject and do not keep a reference to it.

    In the Destroy script above, I have no idea what you might be destroying since it certainly isn't the thing you instantiated and then did not retain a reference to. :)

    It is not necessary to make a script to destroy something. You can just retain a reference to the instantiated dust particle (make sure you aren't recreating a fresh one every frame or you'll have a lot of dust), then destroy it when you stop moving... once.

    In other news, it is a bad idea to call a script Destroy since it confuses with the common .Destroy() method accessible in MonoBehaviours.
     
    Alexandress likes this.
  7. Alexandress

    Alexandress

    Joined:
    May 28, 2018
    Posts:
    5
    I'm still very new to the engine, so I'm not sure what you mean by retaining a reference to.
    The destroy script is something I apply to an object I want to disappear after lifeTime value is equal to 0. It works like a charm for particle since I spawn them and they disappear once the time is over.

    Anyway I've tried not using it, but it has no impact on the Y axis and my issue so yeah optimisation is not what I'm looking for right now

    Thanks
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Could you post a unity package with a small repro? :) Maybe someone can look at it for ya. Maybe me when I'm free...
     
    Alexandress likes this.
  9. Alexandress

    Alexandress

    Joined:
    May 28, 2018
    Posts:
    5
    That was soo dumb from me...
    I just saw that I had a rigidbody activated on the Player. I forgot to remove it when I switched to Character Controller movement. Sorry for the waste of time guys, and thank for the kind help
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, that happens sometimes... :) Glad you got it sorted out.
     
    Alexandress likes this.