Вращение по оси объекта Помогите пожалуйста

Форум для самых маленьких, а так же тех, кому недосуг читать справку самостоятельно.

Вращение по оси объекта Помогите пожалуйста

Сообщение naykz0r 07 апр 2021, 09:17

Есть скрит захвата объекта
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;

public class NewBehaviourScript1 : MonoBehaviour {
       
        private Vector3 screenPoint;
        private Vector3 offset;
       
        void 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));
        }
       
        void OnMouseDrag()
        {
                Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
               
                Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
                transform.position = curPosition;
        }
}
 

и скрипт вращения
Синтаксис:
Используется csharp
public class rotate2 : MonoBehaviour
{
    public GameObject obj;
   public float speed;
   public int VectorX;
 public int VectorY;
 public int VectorZ;
void Update()
    {
        if(Input.GetKey(KeyCode.E))
        obj.transform.rotation *= Quaternion.Euler(VectorX*speed, 0, 0);
if(Input.GetKey(KeyCode.R))
        obj.transform.rotation *= Quaternion.Euler(0, VectorY*speed, 0);
if(Input.GetKey(KeyCode.T))
        obj.transform.rotation *= Quaternion.Euler(0, 0, VectorZ*speed);
    }
}


как сделать так что бы вращался только тот объект который захвачен, а не все которые под скриптом???
naykz0r
UNец
 
Сообщения: 9
Зарегистрирован: 17 мар 2021, 21:22

Re: Вращение по оси объекта Помогите пожалуйста

Сообщение naykz0r 07 апр 2021, 09:54

Вопрос решен рейкастом
Синтаксис:
Используется csharp
Void Update() {
    Vector3 center = new Vector3(Screen.width/2, Screen.height/2, 0);

                RaycastHit hit;
                Ray ray = mainCamera.ScreenPointToRay(center);
if (Physics.Raycast(ray, out hit)) {
if(hit.rigidbody){
if(Input.GetMouseButtonDown(0) && hit.distance < 10) {

hit.transform.Rotate(0, 2f*speed, 0);
}
 
naykz0r
UNец
 
Сообщения: 9
Зарегистрирован: 17 мар 2021, 21:22


Вернуться в Почемучка

Кто сейчас на конференции

Сейчас этот форум просматривают: GoGo.Ru [Bot], Yandex [Bot] и гости: 28