Skip to content

Instantly share code, notes, and snippets.

@vallentiin
Created October 4, 2020 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vallentiin/0cb3430a52d18f97286bc66a39ad6ccf to your computer and use it in GitHub Desktop.
Save vallentiin/0cb3430a52d18f97286bc66a39ad6ccf to your computer and use it in GitHub Desktop.
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