Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- Vector3 playerPosition;
- Vector3 mouseF;
- Vector3 mouseN;
- void FixedUpdate()
- {
- if (Input.GetMouseButton(0))
- {
- setMouseF();
- setMouseN();
- Debug.DrawRay(mouseN, mouseF - mouseN, Color.green);
- Debug.DrawRay(mouseN, (playerPosition - mouseN), Color.red);
- // move the player base on mouse position
- transform.position = mouseF;
- }
- if (Input.GetMouseButtonUp(0))
- {
- // on mouse release, set the new player position
- playerPosition = mouseF;
- }
- }
- void setMouseF()
- {
- Vector3 mouseFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
- mouseF = Camera.main.ScreenToWorldPoint(mouseFar);
- }
- void setMouseN()
- {
- Vector3 mouseNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
- mouseN = Camera.main.ScreenToWorldPoint(mouseNear);
- }
- }
- Vector3 initialMousePos;
- Vector3 offset;
- private void Update() {
- if(Input.GetMouseButtonDown(0))
- {
- initialMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- }
- if(Input.GetMouseButton(0))
- {
- offset = initialMousePos - Camera.main.ScreenToWorldPoint(Input.mousePosition);
- transform.position += offset;
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- Vector3 playerPosition;
- Vector3 mouseF;
- Vector3 mouseN;
- Vector3 diff;
- void FixedUpdate()
- {
- if (Input.GetMouseButtonDown(0))
- {
- InitPlayer();
- }
- if (Input.GetMouseButton(0))
- {
- SetMouseF();
- SetMouseN();
- Debug.DrawRay(mouseN, mouseF - mouseN, Color.green);
- Debug.DrawRay(mouseN, (mouseF + diff) - mouseN, Color.red);
- // move the player base on mouse position
- transform.position = mouseF + diff;
- }
- if (Input.GetMouseButtonUp(0))
- {
- }
- }
- void InitPlayer()
- {
- SetMouseF();
- // set player position where the object is
- playerPosition = transform.position;
- // get the distance between the player and mouse click
- diff = playerPosition - mouseF;
- }
- void SetMouseF()
- {
- Vector3 mouseFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
- mouseF = Camera.main.ScreenToWorldPoint(mouseFar);
- }
- void SetMouseN()
- {
- Vector3 mouseNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
- mouseN = Camera.main.ScreenToWorldPoint(mouseNear);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement