Search Unity

How can I randomly move my gameobject over my screen like dvd on pause

Discussion in '2D' started by SamVorst, Dec 17, 2019.

  1. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Hi,

    I want to make that my gameobject can move randomly like the dvd logo. Anybody know how to do this??

    Thanks Sam
     
  2. Claoudy

    Claoudy

    Joined:
    Jul 14, 2018
    Posts:
    1

    there you go, it's javascript but i think you can easily adapt it to unity
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @SamVorst

    What @Claoudy suggested probably works, but there is even easier way;

    Add rigidbody and collider to your object, and make a Physics2D material for it, set the material friction to 0 and bounciness to 1.

    Remove the gravity from your rigidbody, and in Start set its velocity to some direction.
     
  4. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Hi thanks, I'm coming far using this the only things i don't understand are: how to do velocity setting and how to prevent it not going to turn?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    You can set the velocity using Rigidbody2D.velocity in a script attached to the same GameObject. You can select the Rigidbody2D in the inspector and in the "Constraints" options check the freeze rotation constraint. You can find details on the Rigidbody2D here.
     
  6. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    All right, but I'm very new to coding so i don't know how to do to with only this ...
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Here's a tutorial about how to make an object with bouncy physics:

    Then as @MelvMay said, on the object's rigidbody component you can set the constraints to lock the Z rotation. You'll also want to set gravity scale to 0.

    Then you need a script to set the velocity when the game starts:
    Code (CSharp):
    1. public class InitialVelocity : MonoBehaviour
    2. {
    3.     // X and Y velocity set in the inspector
    4.     public Vector2 initialVelocity;
    5.  
    6.     private void Start()
    7.     {
    8.         // set the rigidbody velocity
    9.         GetComponent<Rigidbody2D>().velocity = initialVelocity;
    10.     }
    11. }
     
    MelvMay likes this.
  8. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I did and the result is that it don't move...
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Did you set the initialVelocity in the inspector to something other than the default of (0,0)?
     
  10. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I don't know this is how it looks now
    upload_2019-12-19_10-18-28.png
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    I suggest you start with some of the basic scripting and editor tutorials to be honest as that'll help you enormously.

    Any public field in a script will automatically show in the inspector. Your "Velocity" script doesn't seem to be showing a public Velocity field as the example does so you've either not added it or you've not made it public. Please just use the example above or copy it exactly. Then you can set the initial velocity.
     
  12. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I used the code above but it doesn't work...
     
  13. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    @MelvMay I copied the code and I still could not to get it work.
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Just saying you couldn't get it to work doesn't help me help you. You could at least say which part didn't work as we were talking about you setting the velocity in the inspector so you could tell me if you managed to do that or not.

    Maybe post the Velocity script. I know you didn't use the exact code above because your component is named differently so maybe you missed something else? If you did manage to set the velocity in the inspector then I'm interested in how you assign that to the Rigidbody2D.

    Basically help me help you by me not having to guess what's not working and what you've already done.
     
  15. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I can't still change the velocity.
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Sorry but you need to ellaborate. Why can't you change the velocity? Please post the script and the inspector for that script.
     
  17. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Velocity : MonoBehaviour
    6. {
    7.     public class InitialVelocity : MonoBehaviour
    8.     {
    9.         // X and Y velocity set in the inspector
    10.         public Vector2 initialVelocity;
    11.  
    12.         private void Start()
    13.         {
    14.             // set the rigidbody velocity
    15.             GetComponent<Rigidbody2D>().velocity = initialVelocity;
    16.         }
    17.     }
    18.  
    19. }
    20.  
    upload_2019-12-19_16-41-22.png
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    So the script you're using isn't the same at all. For some reason you're using the example script but you've placed it inside another MonoBehaviour script. Why are you doing that? Simply use the example shown above and then you'll find the Velocity proprety is shown in the inspector.
     
    LiterallyJeff likes this.
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    If you want the script called "Velocity" then use the following (it's no different than the original example though):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Velocity : MonoBehaviour
    6. {
    7.     // X and Y velocity set in the inspector
    8.     public Vector2 initialVelocity;
    9.  
    10.     private void Start()
    11.     {
    12.         // set the rigidbody velocity
    13.         GetComponent<Rigidbody2D>().velocity = initialVelocity;
    14.     }
    15. }
     
    LiterallyJeff likes this.
  20. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Wow, i'm so stupid i was forgotten to delete the monobehaviour. But I have one more question, If the button hits an collider he goes straigth downstairs how can i prevent that?
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    So I guess it works now? It would be nice for you to confirm that before moving on.

    Anyway, sorry but I have no way of understanding what you mean here. I don't even know what is happening in your project nor what you mean by "downstairs". In the end, it sounds very much like you should follow some tutorials such as this one: https://learn.unity.com/tutorial/2d-physics

    This'll explain the basics including script callbacks for collisions/triggers and how to respond to them.