Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by silviufuicu · Sep 06, 2016 at 01:41 PM · c#movementmobilemousepositionmouseclick

Unity3d 4,script nGUI btn drag convert to move where i mouse click

Hello , i have this script attached to a nGui btn that move the player in the direction where the btn is drag(mobile). I need to change the script to move the player where i mouse click.

Any idea how to do that ?

     using UnityEngine; 
     using System;
     using System.Collections;
     using System.Collections.Generic;
     using UICommon ; 
     using BlGame.GameEntity;
     using BlGame.GameData;
     using JT.FWW.GameData;
     using BlGame.FSM;
     using BlGame.GuideDate;
     using BlGame.Model;
     using BlGame.GameState;
     
     public class VirtualStickUI : MonoBehaviour 
     {
         #region 
         public void SetVirtualStickUsable(bool enable)
         {
             canUse = enable;         
             if (!enable) 
             {
                 CloseStick();    
             }
         }
     
         #endregion
     
         #region
         void Awake()
         {
             Init();
         }
     
         void Init()
         { 
             Instance = this;
             orignalPos = transform.localPosition;
             VirtualStickState = StickState.InActiveState;
             point = transform.FindChild("stick");
             btnSelf = transform.GetComponent<ButtonOnPress>();
             
             //firstInit = true;
         }
     
     
         void OnEnable()
         {        
             canUse = true;
             SetVisiable(false);
             btnSelf.AddListener(PressVirtual, ButtonOnPress.EventType.PressType);
             BlGame.Ctrl.UIGuideCtrl.Instance.AddUiGuideEventBtn(btnSelf.gameObject);
         }
     
         void OnDisable() {
             btnSelf.RemoveListener(PressVirtual, ButtonOnPress.EventType.PressType);
             VirtualStickState = StickState.InActiveState;
         }
      
         
         /// <param name="index">Index.</param>
         /// <param name="isDown">If set to <c>true</c> is down.</param>
         void PressVirtual(int ie,bool isDown)
         {
             if(isDown){
                 ShowStick();
             }else{
                 CloseStick();
             }
         }
     
         void OnDrag(Vector2 pos)
         {
             if(canUse == false) return;
     
             Vector2 touchPos = UICamera.currentTouch.pos;
              
             SetPointPos(touchPos);
     
             VirtualStickState = StickState.MoveState;
             SendMove();
         }
     
    
         void ShowStick()
         {
             if(canUse == false) return;
             //PlayerManager.Instance.LocalPlayer.MoveCount = -1;
             Vector2 touchPos = UICamera.currentTouch.pos;
     
             SetPointPos(touchPos);
     
             VirtualStickState = StickState.MoveState;
             //SendMove();
             transform.position = new Vector3(UICommonMethod.GetWorldPos(touchPos).x, UICommonMethod.GetWorldPos(touchPos).y, transform.position.z);
             point.localPosition = Vector3.zero;
             //VirtualStickState = StickState.ActiveState; 
             SetVisiable(true);         
         } 
     
         void CloseStick()
         {
             if (VirtualStickState == StickState.MoveState)
             {
                 SendStop();
             }
             Iselfplayer player = PlayerManager.Instance.LocalPlayer;
             if (player != null && player.FSM != null && player.FSM.State == FsmState.FSM_STATE_ADMOVE)
             {
                 player.OnFSMStateChange(EntityFreeFSM.Instance);
             }
             SetVisiable(false);
             VirtualStickState = StickState.InActiveState;
             beforeDir = Vector3.zero;
         }
     
         void SendStop(){
             CGLCtrl_GameLogic.Instance.EmsgToss_AskStopMove ();
         }
         
         private const float SendMoveInterVal = 0.05f;
         private float MoveSendTime;
         void SendMove()
         { 
             Vector3 direction = GetPointerDirection();    
             Transform target = JxBlGame.Instance.transform; 
             Entity entity = PlayerManager.Instance.LocalPlayer.RealEntity;
             if (entity == null) {
                 return ;        
             }
     
             PlayState playState = GameStateManager.Instance.GetCurState() as PlayState;
             if (playState == null)
                 return;
     
     
             target.position = entity.transform.position;
             target.LookAt(entity.transform.position + direction);
            
             
             Vector3 dir = new Vector3(0, 0, 0);
     
           
             //target.transform.Rotate(new Vector3(0.0f, 45f, 0.0f));
     
            
             if (playState.mCameraType == 1)      
             {
                 
                 if (entity.CampType == EntityCampType.CampTypeA)
                 {
                     Quaternion rot = Quaternion.Euler(0, 45f, 0);
                     dir = rot * target.forward;
                 }
                 else if (entity.CampType == EntityCampType.CampTypeB)
                 {
                     Quaternion rot = Quaternion.Euler(0, -135f, 0);
                     dir = rot * target.forward;
                 }
                 else
                 {
                     Debug.Log("no valid entity camp type!");
                 }
             }
             else                               
             {
           
                 if (entity.CampType == EntityCampType.CampTypeA)
                 {
                     Quaternion rot = Quaternion.Euler(0, 0f, 0);
                     dir = rot * target.forward;
                 }
                 else if (entity.CampType == EntityCampType.CampTypeB)
                 {
                     Quaternion rot = Quaternion.Euler(0, -180f, 0);
                     dir = rot * target.forward;
                 }
                 else
                 {
                     Debug.Log("no valid entity camp type!");
                 }
             }
                 
             Vector3 dealPos = entity.transform.position + dir * Time.deltaTime * PlayerManager.Instance.LocalPlayer.EntityFSMMoveSpeed;
             Vector3 dealPos1 = dealPos + dir * Time.deltaTime * PlayerManager.Instance.LocalPlayer.EntityFSMMoveSpeed * 2;       
     
     
             if(dir != Vector3.zero && Time.time - MoveSendTime >= SendMoveInterVal)
             {
                 //if (SceneGuideTaskManager.Instance().IsNewsGuide() != SceneGuideTaskManager.SceneGuideType.NoGuide) {
                 //    if (!PlayerManager.Instance.LocalPlayer.CheckGuideCanMove(dealPos1))
                 //    {
                 //        SendStop();
                 //        return;
                 //    }
                 //}
     
                 MoveSendTime = Time.time; 
                 CGLCtrl_GameLogic.Instance.EmsgToss_AskMoveDir(dir);
                 beforeDir = dir ;
                 PlayerAdMove(dir);
             }
         }
     
         private void PlayerAdMove(Vector3 mvDir) {
             Iselfplayer player = PlayerManager.Instance.LocalPlayer;
             if (BlGame.Skill.BuffManager.Instance.isHaveStopBuff(player.GameObjGUID))
             {
                 return;
             }
             if (player.FSM == null)
             {
                 return;
             }
             if (player.FSM.State == FsmState.FSM_STATE_DEAD || player.FSM.State == FsmState.FSM_STATE_RUN || player.FSM.State == FsmState.FSM_STATE_FORCEMOVE
                 || player.FSM.State == FsmState.FSM_STATE_RELIVE)
             {
                 return;
             }
             float mvSpeed = player.EntityFSMMoveSpeed;
             if(mvSpeed <= 0)
             {
                 mvSpeed = 3.0f;
             }
             player.EntityFSMChangedata(player.realObject.transform.position, mvDir, mvSpeed);
             player.OnFSMStateChange(PlayerAdMoveFSM.Instance);
         }
     
         void SetVisiable(bool visiable)
         {
             if (visiable) {
                 UICommonMethod.TweenColorBegin(gameObject, 0f, 1f);
             }else {
                 UICommonMethod.TweenColorBegin(gameObject, 0f, 0.5f);
                 transform.localPosition = orignalPos;
                 point.localPosition = Vector3.zero;
             }
         }
     
         void SetPointPos(Vector2 pos)
         {
             Vector3 newPos = UICommonMethod.GetWorldPos(pos)+new Vector3(0f,0f,point.position.z);
             
             if(Vector3.Distance(newPos,transform.position) > adjustRadius)
             {        
                 Vector3 direction = newPos - transform.position;
                 direction.Normalize();
                 direction *= adjustRadius;
                 newPos = transform.position + direction; 
             }
             point.position = newPos;         
         } 
     
         Vector3 GetPointerDirection()
         {
             Vector3 direction = point.position - transform.position;
             
             direction = new Vector3(direction.x, 0f, direction.y);
             direction.Normalize();
             
             return direction;
         }
         
         Vector3 NormalVector3(Vector3 pos)
         {
             float x = (float)Math.Round(pos.x,1);
             float y = (float)Math.Round(pos.y,1);
             float z = (float)Math.Round(pos.z,1);
             Vector3 posTemp = new Vector3(x,y,z);
             return posTemp;
         }
         
         bool Vect3Compare(Vector3 pos1,Vector3 pos2)
         {  
             if(pos1.x != pos2.x){    
                 return false ;
             }
             if(pos1.y != pos2.y){             
                 return false ;
             }
             if(pos1.z != pos2.z){ 
                 return false ;
             }
             
             return true;
         }
      
         #endregion
     
         #region     
         public static VirtualStickUI Instance {
             get;
             private set;
         }
     
     
         public enum StickState
         {
             ActiveState,
             MoveState,
             InActiveState,
         }
     
         public StickState VirtualStickState{
             get;
             private set;
         }
     
         private Transform point;
     
         private ButtonOnPress btnSelf;
     
         private Vector3 orignalPos = new Vector3();
     
         private bool canUse = true; 
     
         public float adjustRadius = 0.3f;
     
         private Vector3 beforeDir;
         #endregion
     }
 
 



Thanks for your time .

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by silviufuicu · Sep 07, 2016 at 12:19 PM

With unity docs i manage to do convert the script to what i wanted ...

     if (Input.GetMouseButtonDown (0) && GUIUtility.hotControl == 0) {
             
                         Plane playerPlane = new Plane (Vector3.up, myTransform.position);
                         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                         float hitdist = 0.0f;
             
                         if (playerPlane.Raycast (ray, out hitdist)) {
                                 Vector3 targetPoint = ray.GetPoint (hitdist);
                                 destinationPosition = ray.GetPoint (hitdist);
                                 Quaternion targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
                                 myTransform.rotation = targetRotation;




thanks unity for good docs

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How to make an object move to another object's location in a 2d game? 2 Answers

Multiple Cars not working 1 Answer

how to make rail and then make something move on it? 0 Answers

[Problem] Two Mobile Single Stick Controllers and Click Controls not working synonymous with each other 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges