Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BulletBehaviourScript : MonoBehaviour
  6. {
  7.     public Vector2 deltaPosition;
  8.  
  9.     public GameObject sphere;
  10.  
  11.     public Vector3 startMousePosition;
  12.  
  13.     //Declare a variable to store hit data
  14.     private RaycastHit hit;
  15.     private bool activation = true;
  16.  
  17.     public float xForceMultiplier = 0.5f;
  18.     #region Unity Messages
  19.  
  20.     // Function thats called each frame
  21.     private void Update()
  22.     {
  23.         //If user pressed left mouse button
  24.         if (Input.GetMouseButtonDown(0))
  25.         {
  26.            
  27.             //Launch raycast from mouse position and save the data to variable
  28.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
  29.             {
  30.                 // IF object has proper tag
  31.                 if (hit.transform.CompareTag("Interactable"))
  32.                 {
  33.                     //Set start mouse position and show information
  34.                     Debug.Log("Elo mordy");
  35.                     startMousePosition = Input.mousePosition;
  36.                 }
  37.             }
  38.         }
  39.         //If user is pressing left mouse button , update sphere position, corresponding to mouse position
  40.         if (Input.GetMouseButton(0))
  41.         {
  42.             sphere.transform.position = (Input.mousePosition - startMousePosition) / 50;
  43.         }
  44.         //If user lets go of mouse button, calculate force and launch sphere
  45.         if (Input.GetMouseButtonUp(0) )
  46.         {
  47.             deltaPosition = Input.mousePosition - startMousePosition;
  48.             sphere.GetComponent<Rigidbody>().isKinematic = false;
  49.             sphere.GetComponent<Rigidbody>().AddForce(new Vector3(-deltaPosition.x * xForceMultiplier, 2, 2) * 300);
  50.         }
  51.     }
  52.     #endregion
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement