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

player objects disappear when moving forward

Discussion in 'Scripting' started by Northernlightifly, Mar 31, 2018.

  1. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Hi!

    So I try to make my ball move but it keeps disappearing at times I recorded it so you guys can see what's going on. How can I fix this issue?



    Thank you in advance and happy easter!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Looks like it could be the y portion of the rotation and the z position.

    Try starting the game, but don't move like you normally do. In the inspector adjust the y rotation and see if it ever disappears. Then try the z position.

    If one or both are the cause, make sure your code doesn't do that. :)

    Post your code, if you get stuck, and maybe someone can help you further.
     
  3. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Okay, how exacly should I adjust the y rotation? to 0?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, what I originally meant was you may not want to be changing that in code? Though, yes, zero would be 'unchanged'.
     
  5. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Okay! I haven't changed anything in my code or in the z column where everything is set to 0 except for scale should it be set to 0 as well?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No, I don't think the scale matters.

    Note, there's also the z position I mentioned before, though you haven't commented on that. I'm fairly certain that was responsible for one of the "disappearing acts", also.
     
  7. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Oh right! And I click the play button and changed the position in the Y rotation and z position and the ball did disappeared when I did that.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya.. well, just make sure your code is not changing either of those? ;)
     
  9. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    It doesn't!
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    How do they change while you're playing then? ;)
     
  11. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    The code I've written for the ball to move doesn't change at all when I test play it, only all the positions in the transform section change when I try to move the ball.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  13. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Code (CSharp):
    1. public class NewPiskelkontrol : MonoBehaviour
    2.    {
    3.        void Update()
    4.        {
    5.         var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
    6.         var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
    7.  
    8.  
    9.         transform.Rotate(0, x, 0);
    10.         transform.Translate(0, 0, z);
    11.        }
    12.  
    13.  
    14.  
    15.    }
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so it is rotating and on the y and moving on the z...
    That is code better suited for a 3d program, I would imagine.

    For 2D, you'd want to move on the x-axis (in your scene). You can "rotate" your sprite left/right by using a negative/positive x-scale factor, or by using the 'FlipX' property.

    If you're just starting out, I'd suggest that you spend some time working on tutorials.
    You can find lots of good stuff here: https://unity3d.com/learn/tutorials
     
    SomeVVhIteGuy likes this.
  15. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Thank you it worked! I was wondering I have a sun I want to make spining all the time how do I do that?
     
    Last edited: Apr 1, 2018
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can rotate 2D objects on the z-axis. Try it out :)
     
  17. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    I see thank you! Can I make so that it keeps spinning ingame? I guess I do it by making a script for it?
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yes, and yes :)
     
    SomeVVhIteGuy likes this.
  19. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Alright! Then I do like I did with my ball that I mark the sun and add component and choose script?
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, if it's just rotating you can have the script just change the z rotation over time. :)
     
  21. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Okay I tried using this code but it doesn't work and I get error in Unity "All compiler errors have to be fixed before you can enter playmode! UnityEditor.SceneView:ShowCompileErrorNotification()"

    Code (CSharp):
    1. void RotateLeft () {
    2.         transform.Rotate(Vector3.forward * -90);
    3.        
    4.     }
    5.    
    6.     // Update is called once per frame
    7.     void Update () {
    8.        
    9.     }
    10. }
    and also tried this (copied now from unity's website
    Code (CSharp):
    1. public class ExampleClass : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         // Rotate the object around its local X axis at 1 degree per second
    6.         transform.Rotate(Time.deltaTime, 0, 0);
    7.  
    8.         // ...also rotate around the World's Y axis
    9.         transform.Rotate(0, Time.deltaTime, 0, Space.World);
    10.     }
    11. }
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The example looks okay, except that it's not the z-axis. As for the error - what is it?
    The message you pasted in here only shows up as an additional messages, it's not a main one..

    Try to figure that out & fix it and see if the script works. :)
     
  23. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Okay but I have four of the same messengers and nothing else, I tried double click on it and it doesn't work.
     
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That is truly strange. Make sure the console is set to 'clear on play' (and/or just clear it once).
    Then, go edit a file.. like delete a character and re-add it, save it & let unity recompile the script.

    Do you see anything useful in the console at that time?
     
  25. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Alright! I cleared it and edited my script and saved it and the errors didn't show up again though my object won't rotate.

    I tried with the following code though I think I will have to add "public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);" I'm not sure though how I am going to write it in my script properly. Does it work if I just go with "Rotate(float xAngle)"?

    Code (CSharp):
    1. void Update()
    2.     {
    3.         transform.Rotate(Vector3.right * Time.deltaTime);
    4.  
    5.         transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
    6.      
    7.      
    8.     }
    9.  
    10.  
    11.      
    12.     }
     
    Last edited: Apr 3, 2018
  26. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Try this (you can adjust the speed in the inspector):
    Code (csharp):
    1. [SerializeField]
    2. float rotSpeed = 20;
    3. void Update()
    4. {
    5.    transform.Rotate(Vector3.forward * rotSpeed * Time.deltaTime);
    6. }
     
    Northernlightifly likes this.
  27. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    It worked! Thank you very much! :D
     
  28. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome :)
     
  29. lolzsoft

    lolzsoft

    Joined:
    Aug 4, 2021
    Posts:
    4
    Hello everyone, I am facing similar problem, even after trying multiple methods to transform. My complete code is as below wherein my character is disappearing every time when I move:

    void Start()
    {
    anim = GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();

    }

    private void Update()
    {
    if(input != 0)
    {
    anim.SetBool("isRunning", true);
    }
    else
    {
    anim.SetBool("isRunning", false);
    }

    //below if statement reflects that the player character(object) has zero rotation if input is 1. And has 180 degree rotation if input is -1
    if (input > 0)
    {
    //transform.eulerAngles = new Vector3(0, 0, 0);
    transform.rotation = Quaternion.Euler(0f, 0f, 0f);
    }
    else if (input < 0)
    {
    //transform.eulerAngles = new Vector3(0, 180, 0); //Rotation 180 degrees for Player character along the Y axis
    transform.rotation = Quaternion.Euler(0f, 180f, 0f);
    }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
    //storing player's input
    input = Input.GetAxisRaw("Horizontal");

    //Moving player
    rb.velocity = new Vector2(input * speed, rb.velocity.y);

    }
     
  30. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,470
    Necro and cross-post of this.