Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class MouseMove2D : MonoBehaviour {
- public Vector3 mousePosition;
- public float moveSpeed = 0.1f;
- public Camera Cam;
- public LayerMask layers;
- public Transform Raystart;
- [SerializeField] private bool m_ShowDebugRay; // Optionally show the debug ray.
- [SerializeField] private float m_DebugRayLength = 5f; // Debug ray length.
- [SerializeField] private float m_DebugRayDuration = 1f; // How long the Debug ray will remain visible.
- [SerializeField] private float m_RayLength = 500f; // How far into the scene the ray is cast.
- // Use this for initialization
- void Start () {
- Cam = Camera.main;
- }
- // Update is called once per frame
- void Update () {
- //if ( Input.GetMouseButton (1)){ // << use GetMouseButton instead of GetMouseButtonDown
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if ( Physics.Raycast (ray,out hit,100.0f)) {
- // Debug.Log("You selected the " + hit.transform.name);
- mousePosition = hit.point;
- transform.position = hit.point;
- //hit.transform.position += Vector3.right * moveSpeed * Time.deltaTime; // << declare public speed and set it in inspector
- }
- //}
- //mousePosition = Input.mousePosition;
- //mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
- if (m_ShowDebugRay)
- {
- //Debug.DrawRay(Raystart.transform.position, mousePosition * m_DebugRayLength, Color.yellow, m_DebugRayDuration);
- /*
- Ray ray = new Ray (transform.position, transform.forward);
- // Raycast to find a ragdoll collider
- RaycastHit hit = new RaycastHit();
- if (Physics.Raycast(ray, out hit, 100f, layers)) {
- Debug.DrawRay(transform.position, transform.forward * m_DebugRayLength, Color.red, m_DebugRayDuration);
- }
- */
- }
- if (Input.GetMouseButton(0)) {
- //mousePosition = Input.mousePosition;
- //mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
- //transform.position = mousePosition;
- //transform.position = Vector3.Lerp(transform.position, mousePosition, moveSpeed);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement