Вопрос по управлению объектом с помощью мышки
AIDEN Дата: Вс, 13 Июл 2014, 16:20 | Сообщение # 1
Сообщений: 59
Награды:
0
Репутация:
56
Статус: Offline
У меня есть 2 скрипта, один перемещение объекта с помощью мышки : Код
private var screenPoint : Vector3 ; private var offset : Vector3 ; function Start () : void { originRotation = transform . rotation ; screenPoint = Camera . main . WorldToScreenPoint ( transform . position ); } function OnMouseDown () { screenPoint = Camera . main . WorldToScreenPoint ( gameObject . transform . position ); offset = gameObject . transform . position - Camera . main . ScreenToWorldPoint ( new Vector3 ( Input . mousePosition . x , Input . mousePosition . y , screenPoint . z )); } function OnMouseDrag () { var curScreenPoint : Vector3 = new Vector3 ( Input . mousePosition . x , Input . mousePosition . y , screenPoint . z ); var curPosition : Vector3 = Camera . main . ScreenToWorldPoint ( curScreenPoint ); transform . position = curPosition ; }
А второй поворот объекта мышкой : Код
var speed : float = 10.0f ; var sensitivity : float = 10.0f ; private var originRotation : Quaternion ; private var screenPoint : Vector2 ; private var angle : float = 0.0f ; function Start () { originRotation = transform . rotation ; screenPoint = Camera . main . WorldToScreenPoint ( transform . position ); } function Update () { if ( angle > 359.9f ) angle = 0 ; if ( angle < 0 ) angle = 359.9f ; var rotate : Quaternion = originRotation * Quaternion . AngleAxis ( angle , Vector3 . forward ); transform . rotation = Quaternion . Slerp ( transform . rotation , rotate , Time . deltaTime * speed ); } function OnMouseDrag () { var directionX : float = 0f ; var directionY : float = 0f ; if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = 1f ; } angle += Input . GetAxis ( "Mouse X" ) * sensitivity * directionX ; angle += Input . GetAxis ( "Mouse Y" ) * sensitivity * directionY ; }
Когда я нажимаю левую кнопку мышки объект и передвигается, и вращается. Как сделать чтобы при нажатии на левую кнопку мыши объект передвигался, а при нажатии на правую, объект крутился ?
Моя игра - Collect Fruit
Сообщение У меня есть 2 скрипта, один перемещение объекта с помощью мышки : Код
private var screenPoint : Vector3 ; private var offset : Vector3 ; function Start () : void { originRotation = transform . rotation ; screenPoint = Camera . main . WorldToScreenPoint ( transform . position ); } function OnMouseDown () { screenPoint = Camera . main . WorldToScreenPoint ( gameObject . transform . position ); offset = gameObject . transform . position - Camera . main . ScreenToWorldPoint ( new Vector3 ( Input . mousePosition . x , Input . mousePosition . y , screenPoint . z )); } function OnMouseDrag () { var curScreenPoint : Vector3 = new Vector3 ( Input . mousePosition . x , Input . mousePosition . y , screenPoint . z ); var curPosition : Vector3 = Camera . main . ScreenToWorldPoint ( curScreenPoint ); transform . position = curPosition ; }
А второй поворот объекта мышкой : Код
var speed : float = 10.0f ; var sensitivity : float = 10.0f ; private var originRotation : Quaternion ; private var screenPoint : Vector2 ; private var angle : float = 0.0f ; function Start () { originRotation = transform . rotation ; screenPoint = Camera . main . WorldToScreenPoint ( transform . position ); } function Update () { if ( angle > 359.9f ) angle = 0 ; if ( angle < 0 ) angle = 359.9f ; var rotate : Quaternion = originRotation * Quaternion . AngleAxis ( angle , Vector3 . forward ); transform . rotation = Quaternion . Slerp ( transform . rotation , rotate , Time . deltaTime * speed ); } function OnMouseDrag () { var directionX : float = 0f ; var directionY : float = 0f ; if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = 1f ; } angle += Input . GetAxis ( "Mouse X" ) * sensitivity * directionX ; angle += Input . GetAxis ( "Mouse Y" ) * sensitivity * directionY ; }
Когда я нажимаю левую кнопку мышки объект и передвигается, и вращается. Как сделать чтобы при нажатии на левую кнопку мыши объект передвигался, а при нажатии на правую, объект крутился ? Автор - AIDEN Дата добавления - 13 Июл 2014 в 16:20
Altair Дата: Вс, 13 Июл 2014, 17:13 | Сообщение # 2
Unity 3D PRO Developer
Сообщений: 758
Награды:
6
Репутация:
195
Статус: Offline
AIDEN , сделать проверку какая именно кнопка нажата)
Сообщение AIDEN , сделать проверку какая именно кнопка нажата)Автор - Altair Дата добавления - 13 Июл 2014 в 17:13
Altair Дата: Вс, 13 Июл 2014, 17:14 | Сообщение # 3
Unity 3D PRO Developer
Сообщений: 758
Награды:
6
Репутация:
195
Статус: Offline
Код
var speed : float = 10.0f ; var sensitivity : float = 10.0f ; private var originRotation : Quaternion ; private var screenPoint : Vector2 ; private var angle : float = 0.0f ; function Start () { originRotation = transform . rotation ; screenPoint = Camera . main . WorldToScreenPoint ( transform . position ); } function Update () { if ( angle > 359.9f ) angle = 0 ; if ( angle < 0 ) angle = 359.9f ; var rotate : Quaternion = originRotation * Quaternion . AngleAxis ( angle , Vector3 . forward ); transform . rotation = Quaternion . Slerp ( transform . rotation , rotate , Time . deltaTime * speed ); } function OnMouseDrag () { var directionX : float = 0f ; var directionY : float = 0f ; if ( Input . GetButton ( "Fire2" )){ if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = 1f ; } angle += Input . GetAxis ( "Mouse X" ) * sensitivity * directionX ; angle += Input . GetAxis ( "Mouse Y" ) * sensitivity * directionY ; } }
Сообщение Код
var speed : float = 10.0f ; var sensitivity : float = 10.0f ; private var originRotation : Quaternion ; private var screenPoint : Vector2 ; private var angle : float = 0.0f ; function Start () { originRotation = transform . rotation ; screenPoint = Camera . main . WorldToScreenPoint ( transform . position ); } function Update () { if ( angle > 359.9f ) angle = 0 ; if ( angle < 0 ) angle = 359.9f ; var rotate : Quaternion = originRotation * Quaternion . AngleAxis ( angle , Vector3 . forward ); transform . rotation = Quaternion . Slerp ( transform . rotation , rotate , Time . deltaTime * speed ); } function OnMouseDrag () { var directionX : float = 0f ; var directionY : float = 0f ; if ( Input . GetButton ( "Fire2" )){ if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x < screenPoint . x && Input . mousePosition . y > screenPoint . y ) { directionX = - 1f ; directionY = - 1f ; } else if ( Input . mousePosition . x > screenPoint . x && Input . mousePosition . y < screenPoint . y ) { directionX = 1f ; directionY = 1f ; } angle += Input . GetAxis ( "Mouse X" ) * sensitivity * directionX ; angle += Input . GetAxis ( "Mouse Y" ) * sensitivity * directionY ; } }
Автор - Altair Дата добавления - 13 Июл 2014 в 17:14
AIDEN Дата: Вс, 13 Июл 2014, 18:13 | Сообщение # 4
Сообщений: 59
Награды:
0
Репутация:
56
Статус: Offline
Altair, спасибо ). Сейчас попробуюДобавлено (13 Июл 2014, 17:54) --------------------------------------------- - if(Input.GetButton("Fire2")) не работает ((( Я думал может как-то сделать через луч Raycast ?
Добавлено (13 Июл 2014, 18:13) --------------------------------------------- Есть ещё такой скрипт :
Код
using UnityEngine ; using System . Collections ; public class mPanelControl : MonoBehaviour { public Vector3 vectorAxis = Vector3 . up ; private Quaternion originRotation ; private Vector3 oldPoint ; private Ray ray ; public Plane plane ; public float distance = 100f ; public float angle = 0f ; void Start () { plane = new Plane ( vectorAxis , transform . position ); } void OnMouseDown () { ray = Camera . main . ScreenPointToRay ( Input . mousePosition ); if ( plane . Raycast ( ray , out distance )) { oldPoint = ray . GetPoint ( distance ); originRotation = transform . rotation ; } } void OnMouseDrag () { if ( Input . GetMouseButton ( 0 )) { ray = Camera . main . ScreenPointToRay ( Input . mousePosition ); if ( plane . Raycast ( ray , out distance )) { Vector3 newPoint = ray . GetPoint ( distance ); Vector3 vecFrom = oldPoint - transform . position ; Vector3 vecTo = newPoint - transform . position ; angle = Vector3 . Angle ( vecFrom , vecTo . normalized ); Vector3 cross = Vector3 . Cross ( vectorAxis , vecFrom ); if ( Vector3 . Dot ( vecTo , cross ) < distance ) { vectorAxis = - vectorAxis ; } transform . rotation = originRotation * Quaternion . AngleAxis ( angle , vectorAxis ); Debug . DrawLine ( transform . position , oldPoint , Color . blue ); Debug . DrawLine ( transform . position , newPoint , Color . green ); Debug . DrawLine ( oldPoint , newPoint , Color . red ); } } } }
Только что-то он не работает (
Моя игра - Collect Fruit
Сообщение Altair, спасибо ). Сейчас попробуюДобавлено (13 Июл 2014, 17:54) --------------------------------------------- - if(Input.GetButton("Fire2")) не работает ((( Я думал может как-то сделать через луч Raycast ?
Добавлено (13 Июл 2014, 18:13) --------------------------------------------- Есть ещё такой скрипт :
Код
using UnityEngine ; using System . Collections ; public class mPanelControl : MonoBehaviour { public Vector3 vectorAxis = Vector3 . up ; private Quaternion originRotation ; private Vector3 oldPoint ; private Ray ray ; public Plane plane ; public float distance = 100f ; public float angle = 0f ; void Start () { plane = new Plane ( vectorAxis , transform . position ); } void OnMouseDown () { ray = Camera . main . ScreenPointToRay ( Input . mousePosition ); if ( plane . Raycast ( ray , out distance )) { oldPoint = ray . GetPoint ( distance ); originRotation = transform . rotation ; } } void OnMouseDrag () { if ( Input . GetMouseButton ( 0 )) { ray = Camera . main . ScreenPointToRay ( Input . mousePosition ); if ( plane . Raycast ( ray , out distance )) { Vector3 newPoint = ray . GetPoint ( distance ); Vector3 vecFrom = oldPoint - transform . position ; Vector3 vecTo = newPoint - transform . position ; angle = Vector3 . Angle ( vecFrom , vecTo . normalized ); Vector3 cross = Vector3 . Cross ( vectorAxis , vecFrom ); if ( Vector3 . Dot ( vecTo , cross ) < distance ) { vectorAxis = - vectorAxis ; } transform . rotation = originRotation * Quaternion . AngleAxis ( angle , vectorAxis ); Debug . DrawLine ( transform . position , oldPoint , Color . blue ); Debug . DrawLine ( transform . position , newPoint , Color . green ); Debug . DrawLine ( oldPoint , newPoint , Color . red ); } } } }
Только что-то он не работает ( Автор - AIDEN Дата добавления - 13 Июл 2014 в 18:13
Altair Дата: Пн, 14 Июл 2014, 13:27 | Сообщение # 5
Unity 3D PRO Developer
Сообщений: 758
Награды:
6
Репутация:
195
Статус: Offline
AIDEN , можно ещё не mouse drag юзать а например Input.GetAxis("Mouse Y") и так же по иксу и просто добавлять эти значения к transform.rotation при условии что зажата та или иная кнопка) И так же с перемещением)
Сообщение AIDEN , можно ещё не mouse drag юзать а например Input.GetAxis("Mouse Y") и так же по иксу и просто добавлять эти значения к transform.rotation при условии что зажата та или иная кнопка) И так же с перемещением)Автор - Altair Дата добавления - 14 Июл 2014 в 13:27
AIDEN Дата: Ср, 16 Июл 2014, 20:04 | Сообщение # 6
Сообщений: 59
Награды:
0
Репутация:
56
Статус: Offline
Altair, спасибо большое за помощь. Вечером попробую )))
Моя игра - Collect Fruit
Сообщение Altair, спасибо большое за помощь. Вечером попробую ))) Автор - AIDEN Дата добавления - 16 Июл 2014 в 20:04