Recently saw a very novel rocker is pressed, it will appear in the press position, and dragged take the time, to a certain distance throughout rocker will follow them, then test yourself to do a bit of this shake pole
First, let's talk about my rocker preset structure
Where to hang the code does not matter, the key is to know what I wrote publice out GameObject which corresponds with the code lookup can be dragged or
By the time the code will appear in these
public GameObject rockerOnOff;
public GameObject rockerStick;
public GameObject rockerFather;
public GameObject rockerControlGo;
RockerPosition is actually a picture, this picture is I set transparency becomes 0, so can not see, I am in order to activate the time range control rocker in this picture inside
Rocker The rocker is in fact, usually it inactivated state, but just click on the picture in the RockerPosition range, it will be activated, so rockerOnOff is the scene Rocker
rockerStick the rocker lever is moved, added to the public out of rockerStick
rockerFather out the public, is added scene RockerPosition, because behind the mobile object positioning requires
Well, the next step is the code
UnityEngine the using; the using UnityEngine.EventSystems; public class BarGroundMove: MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler { public rockerOnOff the GameObject; // control rocker switch public rockerStick the GameObject; // rocker lever public rockerFather the GameObject; // rocker parent the GameObject rockerControlGo public; // objects joystick control is desired a float RADIUS = 50 ; // distance rocker bar and rocker basemap the Vector2 dirV2; Vector3 dirV3; void OnPointerDown public (PointerEventData eventData) // click event function IPointerDownHandler interface generation { rockerOnOff.SetActive ( to true ); // click when the rocker will activate Vector2 localPos; // this Api: public static bool ScreenPointToLocalPointInRectangle (RectTransform rect, Vector2 screenPoint, camera CAM, OUT Vector2 localPoint); // rockerFather.transform AS RectTransform, local coordinate rockerFather parent class // coordinate points eventData.position this event triggered // eventData.pressEventCamera this event is triggered when the camera // OUT localPos this parameter is the relative coordinate RectTransform rect of Vector2 RectTransformUtility.ScreenPointToLocalPointInRectangle ( rockerFather.transform AS RectTransform, eventData.position, eventData.pressEventCamera, OUT localPos ); rockerOnOff.transform.localPosition = localPos; // activation rocker position is a position relative rockerFather } public void ondrag (PointerEventData eventDataA ) // drag event IDragHandler interface generation function { the Vector2 localPos; // relative to the drag position of the rocker RectTransformUtility.ScreenPointToLocalPointInRectangle ( rockerOnOff.transform AS RectTransform, eventData.position, eventData.pressEventCamera, OUT localPos ); the Vector2 groundPos; // relative to the drag position of the rocker parent class RectTransformUtility.ScreenPointToLocalPointInRectangle ( rockerFather.transform AS RectTransform, eventData.position, eventData.pressEventCamera, OUT groundPos ); / / opposite rocker bar vector obtained, write the whole word is rockerOnOff.transform.localPositio - Vecter2.zero (the rod, the rocker corresponding to the parent class is local coordinates ( 0 , 0 )) the Vector2 GRV2 = rockerOnOff.transform.localPosition; dirV2 {= LocalPos.normalized; // this is a recording unit vector obtained drag, mobile computing the following calculation to facilitate IF (localPos.magnitude> RADIUS) // when the length is greater than the radius of the rod will follow the entire movement of the rocker { rockerOnOff. transform.localPosition = (GRV2 - groundPos) * RADIUS + .normalized groundPos; rockerStick.transform.localPosition = localPos.normalized * RADIUS; } the else { rockerStick.transform.localPosition = localPos; } } public void OnPointerUp (PointerEventData eventDataA) // release the function IPointerUpHandler interface generation event rockerStick.transform.localPosition = Vector3.zero; dirV2 = Vector2.Zero; // this is a raising time, the unit vector is set to 0, so that the following object can not be moved according to the vector rockerOnOff.SetActive ( to false ); } the Start void () { } void the Update () { dirV3.x = dirV2.x; dirV3.z = dirV2.y; dirV3.y = 0 ; rockerControlGo.transform.Translate (new new Vector3 ( 0 , 0 , * dirV3.magnitude * Time.deltaTime 2 .5f)); IF (dirV3 =! Vector3.zero) { rockerControlGo.transform.rotation = Quaternion.Lerp(rockerControlGo.transform.rotation, Quaternion.LookRotation(dirV3),0.05f); } } }