MyUnityTest

 2023.10.12 unityC#  
 2023.09.18 unityC#  
 2023.09.18 unityC#  
 2023.09.15 unityC#  
 2023.09.15 unityC#  
 2023.09.15 unityC#  
 2023.09.14 unityC#  
 2023.09.13 unityC#  
 2023.09.13 unityC#  

OnMouseDrag


using UnityEngine;
public class MouseDrag : MonoBehaviour
{
    private Vector3 objectPoint;
    void OnMouseDrag()
    {
 //Cubeの位置をワールド座標からスクリーン座標に変換して、objectPointに格納
 // Camera.main.WorldToScreenPointは、ワールド座標をスクリーン座標に変換する。
//Camera.main.ScreenToWorldPointは、スクリーン座標をワールド座標に変換する。
   objectPoint = Camera.main.WorldToScreenPoint(transform.position);  
  float screenX = Input.mousePosition.x;
  float screenY = objectPoint.y;
  float screenZ = objectPoint.z;
  Vector3 currentScreenPoint = new Vector3(screenX, screenY, screenZ);
  Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint);
   transform.position = currentPosition;      
    }
}
/////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drag: MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
 {
  this.screenPoint = Camera.main.WorldToScreenPoint(transform.position);
  this.offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }
 void OnMouseDrag()
 {
 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset;
transform.position = currentPosition;
 }
}

月別
固定ページ