Advertisement
gabriele471

Untitled

Mar 29th, 2020
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     [SerializeField] float playerSpeed = 1f;
  8.     public Rigidbody2D rb;
  9.     public Camera cam;
  10.     Vector3 mousePos;
  11.     public Animator animator;
  12.  
  13.  
  14.     [SerializeField] GameObject fireballPrefab;
  15.    
  16.  
  17.     private void Start()
  18.     {
  19.         rb = this.GetComponent<Rigidbody2D>();
  20.     }
  21.     void Update()
  22.     {
  23.        
  24.  
  25.         movement();
  26.  
  27.         if (Input.GetButtonDown("Fire1"))
  28.         {
  29.             fire();
  30.         }
  31.        
  32.        
  33.     }
  34.     public void fire()
  35.     {
  36.        
  37.  
  38.         GameObject bullet = Instantiate(fireballPrefab, Transform.Position, Quaternion.identity);
  39.        
  40.     }
  41.     public void movement()
  42.     {
  43.        
  44.         Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  45.        
  46.         if (mousePos.x < rb.position.x)
  47.         {
  48.             transform.localScale = new Vector3(-1, 1, 1);
  49.         }
  50.         if (mousePos.x > rb.position.x)
  51.         {
  52.             transform.localScale = new Vector3(1, 1, 1);
  53.         }
  54.  
  55.         animator.SetFloat("speed", mousePos.z);
  56.  
  57.         if (Input.GetKey(KeyCode.W))
  58.         {
  59.             mousePos.z = transform.position.z;
  60.             transform.position = Vector3.MoveTowards(transform.position, mousePos, playerSpeed * Time.deltaTime);
  61.             animator.SetFloat("speed", mousePos.x);
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement