Created
October 4, 2020 15:42
-
-
Save vallentiin/0cb3430a52d18f97286bc66a39ad6ccf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour | |
{ | |
public float mouseSensitivityX = 1; | |
public float mouseSensitivityY = 1; | |
public float walkSpeed = 6; | |
Vector3 moveAmount; | |
Vector3 smoothMoveVelocity; | |
Rigidbody rb; | |
public GameObject shipholder; | |
private GameObject spawnManag; | |
public Camera CameraMain; | |
public const string spawn = "spawnManage"; | |
void Awake() | |
{ | |
Cursor.lockState = CursorLockMode.Locked; | |
Cursor.visible = false; | |
rb = GetComponent<Rigidbody>(); | |
spawnManag = GameObject.FindGameObjectWithTag(spawn); | |
CameraMain = Camera.main; | |
} | |
void Update() | |
{ | |
RotateObjectTowards(); | |
float inputX = Input.GetAxisRaw("Horizontal"); | |
float inputY = Input.GetAxisRaw("Vertical"); | |
Vector3 moveDir = new Vector3(inputX, 0, inputY).normalized; | |
Vector3 targetMoveAmount = moveDir * walkSpeed; | |
moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, .15f); | |
} | |
void FixedUpdate() | |
{ | |
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime; | |
rb.MovePosition(rb.position + localMove); | |
} | |
private void OnCollisionEnter(Collision collision) | |
{ | |
if (collision.gameObject.tag == "mediumAsteroid" || collision.gameObject.tag == "smallAsteroid") | |
{ | |
GetComponent<Health>().Incrementlife(1); | |
} | |
if (collision.gameObject.tag == "bigAsteroid") | |
{ | |
GetComponent<Health>().Incrementlife(2); | |
} | |
} | |
void RotateObjectTowards() | |
{ | |
shipholder.transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment