Search Unity

Need help with my Code

Discussion in 'Scripting' started by tutukas33, Dec 24, 2016.

  1. tutukas33

    tutukas33

    Joined:
    Apr 7, 2013
    Posts:
    28
    Hey. I am trying to make a script that allows me to drag a object on x axis (which works)but for some reason it transforms on y axis... how to make that y axis would be locked i tried adding a rigidbody but the constrains are not working....
    THIS IS MY SCRIPT FOR MOVING IT:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TouchControl : MonoBehaviour {
    6.     float distance = 10;
    7.     void OnMouseDrag()
    8.     {
    9.         Vector3 mousePosition = new Vector3 (Input.mousePosition.x, 250, distance);
    10.         Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
    11.         transform.position = objPosition;
    12.  
    13.     }
    14. }  
    15.  
    Any help would be nice :) Thanks.. Happy Christmas :D
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,470
    you should be passing the Input.MousePosition DIrectly in the ScreenToWorldPoint method, than when you apply that position to the object just ignore the z and y axis.
     
    tutukas33 and djfunkey like this.
  3. tutukas33

    tutukas33

    Joined:
    Apr 7, 2013
    Posts:
    28
    Could you simplify this for me cauz i'm kinda new to c#....Sorry
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,470
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TouchControl : MonoBehaviour {
    4.     private void OnMouseDrag() {
    5.         Vector3 objPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    6.         transform.position = new Vector3(objPosition.x, transform.position.y, transform.position.z);
    7.     }
    8. }
    9.  
    also is a little more future proof but and requires more work, but the more optimial way would to to use the IDragHandler

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class TouchControl : MonoBehaviour, IDragHandler {
    5.     public void OnDrag(PointerEventData eventData) {
    6.         Vector3 objPosition = Camera.main.ScreenToWorldPoint(eventData.position);
    7.         transform.position = new Vector3(objPosition.x, transform.position.y, transform.position.z);
    8.     }
    9. }
    10.  
    but doing so also requires you to have a event system in your scene, and a physics raycaster on your camera to work.
     
    Last edited: Dec 24, 2016
    tutukas33 likes this.
  5. tutukas33

    tutukas33

    Joined:
    Apr 7, 2013
    Posts:
    28
    Thanks!!!!!! <3
     
  6. tutukas33

    tutukas33

    Joined:
    Apr 7, 2013
    Posts:
    28
    Yeah i just ran in to another problem the object i am trying to move when i click on it is transforming on x not to much just a bit(I can move it on x)..... and its kinda annoying...
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Read the developer network rules please, it's very important to have actually helpful thread titles
     
unityunity