Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class WebMovement : MonoBehaviour
- {
- MousePosition3D mousePosition3d;
- Transform playerTransform;
- Vector2 raycastDirection;
- LineRenderer lr;
- bool pressed;
- RaycastHit2D hit;
- [SerializeField] LayerMask webHitMask;
- public GameObject playerObj;
- private void Start()
- {
- lr = GetComponent<LineRenderer>();
- mousePosition3d = GetComponent<MousePosition3D>();
- }
- public void updateWebDisplay(InputAction.CallbackContext context)
- {
- //by default unitys new input system calls 3 times and the event that it calls takes a parameter of callbackContext
- //-------------------------------------------------------------------------------------------------------------------
- //(basically it lets you seperate the button press into 3 phases)
- // phase 1: started = button has just been pressed
- // phase 2: performed = button is being held
- // phase 3: canceled = button has been let go
- //each phase has a bool associated with it accessed by context.started etc...
- // the problem is this only happens once
- //if button is down and player variable is set we do the button stuff
- if(context.performed && playerObj != null)
- {
- lr.enabled = true;
- //need to convert transforms to direction from the player to send the ray
- raycastDirection = playerObj.transform.position + transform.position;
- //do the raycast and store hit info
- hit = Physics2D.Raycast(playerObj.transform.position,raycastDirection,webHitMask);
- pressed = true;
- // if button is down and player variable isn't set we tell the console
- }else if(context.performed && playerObj == null)
- {
- Debug.Log("Player is null");
- //if button isnt pressed and player is set we need to stop the button stuff
- }else if(!context.performed && playerObj != null)
- {
- pressed = false;
- }
- }
- // Update is called every frame, if the MonoBehaviour is enabled.
- private void Update()
- {
- if(pressed)
- {
- //we need to display the "web" as a line between the player and cursor
- lr.SetPosition(0,playerObj.transform.position);
- lr.SetPosition(1,hit.point);
- //this debug is returning player object i wonder if we can ignore it
- Debug.Log(hit.collider.name);
- }else
- {
- //stop the web stuff and add a force
- lr.enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment