jamieTheCoder

WebMovement

Sep 23rd, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5.  
  6. public class WebMovement : MonoBehaviour
  7. {
  8.     MousePosition3D mousePosition3d;
  9.     Transform playerTransform;
  10.     Vector2 raycastDirection;
  11.     LineRenderer lr;
  12.     bool pressed;
  13.     RaycastHit2D hit;
  14.     [SerializeField] LayerMask webHitMask;
  15.     public GameObject playerObj;
  16.     private void Start()
  17.     {
  18.         lr = GetComponent<LineRenderer>();
  19.         mousePosition3d = GetComponent<MousePosition3D>();
  20.     }
  21.     public void updateWebDisplay(InputAction.CallbackContext context)
  22.     {
  23.         //by default unitys new input system calls 3 times and the event that it calls takes a parameter of callbackContext
  24.         //-------------------------------------------------------------------------------------------------------------------
  25.             //(basically it lets you seperate the button press into 3 phases)
  26.                 // phase 1: started = button has just been pressed
  27.                
  28.                 // phase 2: performed = button is being held
  29.                
  30.                 // phase 3: canceled = button has been let go
  31.                
  32.         //each phase has a bool associated with it accessed by context.started etc...
  33.         // the problem is this only happens once
  34.            
  35.         //if button is down and player variable is set we do the button stuff
  36.         if(context.performed && playerObj != null)
  37.         {
  38.             lr.enabled = true;
  39.             //need to convert transforms to direction from the player to send the ray
  40.             raycastDirection = playerObj.transform.position + transform.position;
  41.             //do the raycast and store hit info
  42.             hit = Physics2D.Raycast(playerObj.transform.position,raycastDirection,webHitMask);
  43.             pressed = true;
  44.         // if button is down and player variable isn't set we tell the console 
  45.         }else if(context.performed && playerObj == null)
  46.         {
  47.             Debug.Log("Player is null");
  48.            
  49.         //if button isnt pressed and player is set we need to stop the button stuff
  50.         }else if(!context.performed && playerObj != null)
  51.         {
  52.             pressed = false;
  53.         }
  54.        
  55.        
  56.     }
  57.     // Update is called every frame, if the MonoBehaviour is enabled.
  58.     private void Update()
  59.     {
  60.         if(pressed)
  61.         {
  62.        
  63.             //we need to display the "web" as a line between the player and cursor
  64.             lr.SetPosition(0,playerObj.transform.position);
  65.             lr.SetPosition(1,hit.point);
  66.            
  67.             //this debug is returning player object i wonder if we can ignore it
  68.             Debug.Log(hit.collider.name);
  69.         }else
  70.         {
  71.             //stop the web stuff and add a force
  72.             lr.enabled = false;
  73.         }
  74.            
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment