Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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