Search Unity

Drag Object in Y-Axis only.

Discussion in '2D' started by RoyWilliams, Nov 2, 2016.

  1. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    11
    I've created an architectural app and I want the client to be able to lift up the roof off the building. I have a script that allows me to drag the roof all over the screen in the X & Y axis:

    using UnityEngine;
    using System.Collections;
    public class mouseDrag_y_axis : MonoBehaviour
    {
    void OnMouseDrag()
    {
    Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 6.33f); //
    Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
    transform.position = objPosition;
    }
    }

    It works but what I really want is that the roof can be lifted up on the Y Axis alone. Looking on Google yealed nothing with regards to just moving the object on just the Y-Axis.

    It must be very straightforward to do but my skill is 3D animation, not coding. So how can I do this, (there's some free 3d modeling in it for anyone that can keep me from tearing out my hair further!) Thanks.
     
    TheDevloper and Reyfire like this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Insert

    Code (csharp):
    1. objPosition.x = transform.position.x;
    2. objPosition.z = transform.position.z;
    right before you assign this result to transform.position at the end of that method.
     
  3. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    11
    Works like a charm. Thanks a million mate. Let me know if you need anything modeled and if I can it's done. Cheers.
     
    JoeStrout likes this.
  4. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    11
    I realise now that the roof object when clicked moves in relation to where the camera is. No doubt because of this line here:

    Code (CSharp):
    1. Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
    So how do I make it that the cube will move up and down without it having anything to do with where the camera is located. The app is AR based so where the camera is when the roof object is clicked is in all different positions and I see as a result, the roof instantly disappears when clicked.

    Thanks again.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Right. You don't want to instantly move the roof to where a line through the camera crosses the view plane; you want to move the roof up and down as the mouse moves up and down.

    Try something like this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class mouseDrag_y_axis : MonoBehaviour {
    4.     public float dragSpeed = 1f;
    5.     Vector3 lastMousePos;
    6.    
    7.     void OnMouseDown() {
    8.         lastMousePos = Input.mousePosition;
    9.     }
    10.    
    11.     void OnMouseDrag() {
    12.         Vector3 delta = Input.mousePosition - lastMousePos;
    13.         Vector3 pos = transform.position;
    14.         pos.y += delta.y * dragSpeed;
    15.         transform.position = pos;
    16.         lastMousePos = Input.mousePosition;
    17.     }
    18. }
     
  6. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    11
    Works great, thanks. Anyway to change the sensitivity? I tried to change to dragSpeed but it didn't make any difference.
     
    TheDevloper likes this.
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Erm... hmm. That's exactly what dragSpeed is for. We're multiplying that by the mouse delta, so it's hard to see why it wouldn't make any difference. Perhaps you didn't change it enough?
     
  8. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    How can I stop when object collides with other collider? I'll continue dragging but object has collided with other collider and further does not moves. How can I do this? Thx
     
  9. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    hii Joe, I used your script for dragging to Y axis the gameobject how can I limit the drag like when it hit an endpoint upwards you wont be able to drag it upwards same thing when downwards.. need help..
    1. using UnityEngine;
    2. using System.Collections;
    3. public class mouseDrag_y_axis : MonoBehaviour {
    4. public float dragSpeed = 1f;
    5. Vector3 lastMousePos;
    6. void OnMouseDown() {
    7. lastMousePos = Input.mousePosition;
    8. }
    9. void OnMouseDrag() {
    10. Vector3 delta = Input.mousePosition - lastMousePos;
    11. Vector3 pos = transform.position;
    12. pos.y += delta.y * dragSpeed;
    13. transform.position = pos;
    14. lastMousePos = Input.mousePosition;
    15. }
    16. }
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You could do this with a couple of if statements, but an even easier way is to use the Mathf.Clamp function. So it'd look like this:
    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.     public class mouseDrag_y_axis : MonoBehaviour {
    4.         public float dragSpeed = 1f;
    5.         public float minY = -5;
    6.         public float maxY = 10;
    7.         Vector3 lastMousePos;
    8.      
    9.         void OnMouseDown() {
    10.             lastMousePos = Input.mousePosition;
    11.         }
    12.      
    13.         void OnMouseDrag() {
    14.             Vector3 delta = Input.mousePosition - lastMousePos;
    15.             Vector3 pos = transform.position;
    16.             pos.y += delta.y * dragSpeed;
    17.             pos.y = Mathf.Clamp(pos.y, minY, maxY);
    18.             transform.position = pos;
    19.             lastMousePos = Input.mousePosition;
    20.         }
    21.     }
    This is simple but maybe not quite ideal, because if you drag quite a ways past the limit, and then drag back in the other direction, the object immediately starts moving — you might instead want it to wait until the mouse has crossed the limit point again. That's certainly doable too; let me know if you want to see that version.
     
  11. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    when I first tap the gameobject it goes down automatically without dragging it and one last thing how can I make may player when standing above the gameObject and start dragging it upwards it also drags my player upwards.. really appreciate your help
     

    Attached Files:

  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, that's a bit trickier. Are you using Physics? (I.e., do the thing you're dragging, and the player, both have a Rigidbody component?)
     
  13. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    my player has a rigidbody only attached
     
  14. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    Hi, Im having a problem dunno what is happening I set the position just like the first picture but when I touch the cube without starting dragging this happens on second pic.. the limit endpoint not working :(.. Can I see the if condition for limit?
     

    Attached Files:

    Last edited: Oct 18, 2018
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Are you sure you set minY and maxY correctly?
     
  16. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    minY is the default position right? and the maxY is far you drag the object.
     
  17. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, those are the minimum and maximum Y values you want to allow. The code does not let the Y position of the object ever be less than minY, or greater than maxY. So you need to set those correctly for the valid range of Y values.
     
  18. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    aside from dragging it to Y axis how can I make it drag to Z and X axis at the same time.. thank you so much your response is a big help for me :)
     
  19. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well I'm not even sure what that means. You can only move the mouse in 2 dimensions; how exactly do you want this to change the position of the object in 3 dimensions?

    If you can define that clearly, you can probably code it. You just need some more lines like lines 16-17, that affect the other parts of pos.
     
  20. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    I got this script to be able to drag my player with the platform but when the player walk inside the platform and tried to walk outside the platform it doesnt work it just stays inside the platform

    private GameObject target = null;
    private Vector3 offset;

    void Start()
    {
    target = null;
    }

    void OnTriggerEnter (Collider col)
    {
    target = col.gameObject;
    offset = target.transform.position - transform.position;
    }

    void OnTriggerExit (Collider col)
    {
    target = null;
    }

    void LateUpdate()
    {
    if (target != null)
    {
    target.transform.position = transform.position + offset;
    }
    }
     
  21. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Please use code tags when posting code. It's the button in the toolbar that looks like this:
    upload_2018-10-23_6-31-28.png
    It makes the code much easier to read.

    So it looks like you're not using physics. That's a reasonable choice! Just be sure to be consistent about it, and don't use rigidbodies to move things elsewhere, or you'll get into some trouble.

    Now the script above basically says, "when something enters my trigger, then keep that thing pinned to me, at its original relative position, until it exits." It's a pretty clever solution, except that it has the problem you mentioned: the pinned thing can't exit once it enters. (It also can't work with more than one object at a time.)

    Perhaps you want something like this version, which applies a delta movement to all targets whenever it moves. This is untested code, right out of my head, so you might need to debug it a little bit.

    Code (CSharp):
    1. private List<Transform> targets = new List<Transform>();
    2. private Vector3 lastPos;
    3.  
    4. void Start() {
    5.     lastPos = transform.position;
    6. }
    7.  
    8. void OnTriggerEnter (Collider col) {
    9.     targets.Add(col.transform);
    10. }
    11.  
    12. void OnTriggerExit (Collider col) {
    13.     targets.Remove(col.transform);
    14. }
    15.  
    16. void LateUpdate() {
    17.     Vector3 delta = transform.position - lastPos;
    18.     if (delta != Vector3.zero) {
    19.         foreach (var target in targets) {
    20.             target.position += delta;
    21.         }
    22.     }
    23.     lastPos = transform.position;
    24. }
     
  22. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    this happens everytime I lift the platform
     

    Attached Files:

  23. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I can't tell from that picture what the trouble is. Can you describe what's going wrong?
     
  24. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    it looks like it doesn't lift my player properly along with the platform. it lift a little bit and then start to sink from the platform just like the image
     
  25. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Are you using physics on your player? How does your player move around? What you're describing sounds like wrestling with the physics engine to me.
     
  26. PrettyLyn

    PrettyLyn

    Joined:
    Aug 6, 2017
    Posts:
    11
    I used Touch To Move for movement of my Player. It has a Rigidbody and NavMesh component.

    Code (CSharp):
    1. void StartMoving()
    2.     {
    3.         if (StopWalking)
    4.         {
    5.             return;
    6.         }
    7.  
    8.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    9.         RaycastHit hit;
    10.        
    11.         if (Input.GetMouseButtonUp (0))
    12.         {
    13.             if (Physics.Raycast (ray, out hit, 100))
    14.             {
    15.                 NavMeshHit navmeshHit;
    16.                 int walkableMask = 1 << NavMesh.GetAreaFromName("Walkable");
    17.                 if (NavMesh.SamplePosition (hit.point, out navmeshHit, 1.0f, walkableMask))
    18.                 {
    19.                     agent.SetDestination (navmeshHit.position);
    20.                 }
    21.  
    22.                 if (hit.collider.CompareTag("Obstacle"))
    23.                 {
    24.                     CanMove = false;
    25.                     Debug.Log("Obstacle");
    26.                 }
    27.  
    28.                 if (hit.collider.CompareTag("Gate"))
    29.                 {
    30.                     CanMove = false;
    31.                     Debug.Log("Gate");
    32.                 }
    33.             }
    34. }
     
  27. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Ah, so that's it. You're not only using physics, you're using NavMesh movement too!

    I don't know how to make it properly stick to the platform in this case. In fact, I'm not sure it's even valid to use a NavMesh when your geometry is changing — my understanding is that these are static (I've never used Unity's navmesh stuff myself). So I think you may be out of luck with that combination.
     
  28. JorgeCobo

    JorgeCobo

    Joined:
    May 29, 2017
    Posts:
    3
    Hey, thank you very much for your answers, they have helped me a lot.

    However, if you have some free time it would help me even more if you could put the script you are talking about at the end of this answer.

    Thank you very much.
     
    Last edited: Oct 27, 2018
  29. neacsucristian98

    neacsucristian98

    Joined:
    Nov 30, 2018
    Posts:
    11
    Man, I was searching for that S*** in the past 2 days, thank you a lot bro!!