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. Dismiss Notice

1st post - Needing some help to adapt games to Unity

Discussion in '2D' started by RenatoB, Mar 28, 2016.

  1. RenatoB

    RenatoB

    Joined:
    Mar 27, 2016
    Posts:
    36
    I develop games using another engine/program and after some tries I really decided to migrate definitely to Unit. Despite I've studied python and know something about javascript, I'm not a programmer, but I think that I'm prepared to be one. Inclusivily, as I wrote in the post's subject, I'm using another engine that it's not necessary to program but that requires logic,and it surelly will help me enough to achieve my goal. In the begin, I intend to convert some games that I produced to Unity format and I'll need some help. Firstly, I would like to know how I do to move an object (1) automatically and change its directions when it collide with other object (2) according its angle. I also would like to change the angle of the object 2 to 90 degrees everytime I touch it. Thanks for any help and sorry about my english.
     
  2. Frostraver

    Frostraver

    Joined:
    Oct 26, 2012
    Posts:
    25
  3. RenatoB

    RenatoB

    Joined:
    Mar 27, 2016
    Posts:
    36
    I'm already doing the tutorials but I'll learn faster if I can see the implementations to adapt some technical features of my games at the same time.

    Edited: I know that I need to create a script for the object1 and I also know how to include the colliders and the rigidbody2d. I definitely don't know how to change the directions. I've a doubt about the object's movement: maybe I need to include a command to change its position (transform.position?) in the "update" function to move it automatically.
     
    Last edited: Mar 28, 2016
  4. Frostraver

    Frostraver

    Joined:
    Oct 26, 2012
    Posts:
    25
    If you're using a rigidbody you usually never want to use transform.position to update the position. You have two choices as far as I know. Or, you use physics AddForce() or you use rigidbodyComponent.velocity. In my game I'm currently using a velocity change. As you might expect the result of this value may vary based on the settings of the RigidBody2D component, so you probably want to change those a bit.

    So, to give a better example if the x-axis is horizontal and y-axis vertical and you want the player to move up you would have a direction of Vector2(0, 1). Now, you can multiply this value with the speed of the object (let's say 20), this would give you Vector2(0, 1) * 20. The most important thing you'll need to do with this is multiply by Time.deltaTime because otherwise your speed will vary based on your framerate. So the final code would be:

    Code (CSharp):
    1. rigidbodyComp.velocity += new Vector2(0, 1) * 20.0f * Time.deltaTime;
    I hope this gets you on your way.
     
  5. RenatoB

    RenatoB

    Joined:
    Mar 27, 2016
    Posts:
    36
    Thank you, Frostraver

    It help me a lot. My code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class movement : MonoBehaviour {
    4.  
    5.     public float speed;
    6.     public int angle;
    7.  
    8.     private Rigidbody2D object1;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         object1 = GetComponent<Rigidbody2D> ();
    14.     }
    15.  
    16.  
    17.     void Update ()
    18.     {
    19.         object1.velocity += new Vector2(1, 0) * speed * Time.deltaTime;
    20.     }
    21. }
    Now, if you could continue to help me, I need to know how to change the object1's direction (changing the angle of motion, I guess) when it collide with the object2. Please, don't complement my script. Just do what you did in the previous post. It was very didactic.
     
  6. Frostraver

    Frostraver

    Joined:
    Oct 26, 2012
    Posts:
    25
    What I would try is add the OnCollisionEnter2D method to this script you wrote above (this is if you're working in 2D, there's similar methods if you're working in 3D). And inside this method I would use this: Vector2.Reflect. You should give it the direction of your object and the normal of the edge of the object you're colliding with, which you can probably do using this information: ContactPoint2D

    I don't have time to test this but this should get you on your way at least
     
  7. RenatoB

    RenatoB

    Joined:
    Mar 27, 2016
    Posts:
    36
    I think it it is more complicated. Thanks anyway. I'll give it a try.
     
    Last edited: Mar 28, 2016
  8. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    If you simply want your rigibody2D object to bounce off another, do this:
    1. Crate a new physics 2D material.
    2. set it's "bouncienes" to 1 or more.
    3. Assign it to your collider's material slot.
    Random direction on click is a little more complex:
    1. Add the code below to your script and attach it to the object.
    2. Add an Event System to the scene (GameObject > UI > Event System).
    3. Add a Physics 2D Raycaster to the camera you're using (Add Component > Event > Physics 2D Raycaster).
    4. Add an Event Trigger to the object you want to click on (Add Component > Event > Event Trigger).
    5. Click Add New Event Type on the Event Trigger and Select PointerDown to add that event to the trigger.
    6. Click the + sign on the PointerDown event to add a listener to that event. You should now see a new slot.
    7. Click & drag the header of the script component (from step 1) in the inspector and drop it on the new event trigger slot.
    8. Select the drop down that says "No Function" on the event slot, then select the script in the list and then it's function (e.g. RandomDirection(Float));
    9. You'll see that you can adjust the force/speed right in the event! Make sure it's more than zero.
    10. Now play the scene and click on the object to test it.

    Code (CSharp):
    1. public void RandomDirection(float speed = 10)
    2.     {
    3.         // Create random velocity value.
    4.         Vector2 randomDirection = Random.insideUnitCircle.normalized;
    5.  
    6.         // Set rigidbody to exact velocity.
    7.         object1.velocity = randomDirection * speed;
    8.     }
    Edit: You should also use FixedUpdate() instead of Update() when controlling physics. However, directly controlling the movement of a rigibody in either is not recommend! It's much better to set the velocity on start or give it a constant force.
     
    Last edited: Mar 29, 2016
  9. RenatoB

    RenatoB

    Joined:
    Mar 27, 2016
    Posts:
    36
    Thanks
    Thanks for all the explanations and suggestions. Despite I need something a little different, I'm in the learning stage and it will be very useful.

    In my project I don't need that the object bounce off. The object just change its direction (turning left/right/opposite direction) when it collide with a trigger object (like arrows, for example). Imagine that I've a tile-mapped board and I've to drag an arrow for one place of this board and after drop it, I can touch to rotate this arrow according with the direction that I want. Thereafter I'll touch in the object1 to start the movement. When the object1 overlap/collide with the arrow, the object1 change your direction to the same direction of the arrow. That's it.
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    In that case, I don't think you need (or want) physics at all. And you may not even need colliders — if your world is composed of a grid, for example, don't be shy about simply doing the math to figure out which grid cell(s) an object is touching yourself.

    When not using physics, you control the position of your object by directly assigning to transform.position, and similarly use transform.rotation to set the rotation. It's super-simple... though it does of course mean you have to code all the motion yourself. But as a benefit, you get to code all the motion yourself. :)

     
  11. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    This may very well be true. It really all depends on what kind of behavior you want. Simple grid like moment is probably best done manually. Since physics are a bit complex and unpredictable.

    Here's one way to do that. First off, change your object's code to something like this instead:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Movement : MonoBehaviour
    4. {
    5.     public Vector2 startingVelocity;
    6.     private Rigidbody2D rb2D;   // give the rigidbody variable a more descriptive name.
    7.  
    8.  
    9.     void Start()
    10.     {
    11.         rb2D = GetComponent<Rigidbody2D>();
    12.     }
    13.  
    14.     // Call this from an event trigger to star moving the ball.
    15.     public void StartMoving()
    16.     {
    17.         rb2D.velocity = startingVelocity;
    18.     }
    19. }
    Now all you need to do is add an event trigger on the object with a PointerDown event that calls the StartMoving() fucniton to start moving the object when clicked.

    Next create another object as your arrow and attach the script below to it. Note: the arrow sprite/model should be pointing to the right.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// Attach this to a Collider2D with IsTrigger checked.
    5. /// </summary>
    6. [RequireComponent(typeof(Collider2D))]
    7. public class ChangeDirection : MonoBehaviour
    8. {
    9.     void OnTriggerEnter2D(Collider2D other)
    10.     {
    11.         // Ignore objects not matching tag.
    12.         // Change this tag to whatever you want.
    13.         if (!other.gameObject.CompareTag("Player"))
    14.             return;
    15.  
    16.         // OPTIONAL: Place object at the center of the trigger.
    17.         // Isn't smooth, but keeps object path predictable.
    18.         other.transform.position = transform.position;
    19.  
    20.         // Set object's direction to the (relative/local) right side of trigger.
    21.         // Using object's magnitude here to maintain speed, but
    22.         // you could also set the object's speed to anything you want.
    23.         other.attachedRigidbody.velocity = transform.right * other.attachedRigidbody.velocity.magnitude;
    24.     }
    25.  
    26.     // Call this from an event trigger to rotate trigger and thous
    27.     // change the direction the object entering this trigger will go.
    28.     public void Rotate(float amount = 90f)
    29.     {
    30.         transform.Rotate(Vector3.forward * amount);
    31.     }
    32. }
    Then once again create a event trigger with a PointerDown event that calls the Rotate() function above, to rotate the arrow when clicked.

    Tip: You can also rotate the arrow in the editor to change it's direction!
    Tip: Make sure gravity = 0 on all your Rigidbody2D objects.
     
    Last edited: Mar 29, 2016
    JoeStrout likes this.
  12. RenatoB

    RenatoB

    Joined:
    Mar 27, 2016
    Posts:
    36
    Thanks about the suggestions @JoeStrout

    Thanks @Isaiah Kelly. I'll try to implement it. A question: what I've to do to rotate the arrow when the player touch it?
     
  13. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    Very easy:
    • Call Rotate(90) at the start of OnTriggerEnter2D() to rotate the arrow before pushing it.
    • Or call Rotate(90) at the end of OnTriggerEnter2D() to rotate the arrow after pushing it.
    • Or call Rotate(90) anywhere in OnTriggerExit2D() to rotate the arrow after pushing it (gives slight delay).
    Edit: by "player" I assume you mean the object being pushed by the arrow, right?
     
    Last edited: Mar 30, 2016
  14. RenatoB

    RenatoB

    Joined:
    Mar 27, 2016
    Posts:
    36
    Nice. I'll give it a try and come back later with the results. Thanks @Isaiah Kelly