Commit 9552248a authored by 15박보승's avatar 15박보승 Committed by Merseong

카메라 컨트롤/간단한 플레이어 컨트롤 구현

parent f9b94557
This diff is collapsed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform target;
[SerializeField]
private float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
private Camera cam;
private void Start()
{
cam = GetComponent<Camera>();
}
private void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY = Mathf.Clamp(currentY - Input.GetAxis("Mouse Y"), -60, 60);
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
transform.position = target.position + rotation * dir;
target.rotation = Quaternion.Euler(0, currentX, 0);
transform.LookAt(target.position);
}
}
fileFormatVersion: 2
guid: 3cff733ed1be50a42b24f5f20109ab7a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -6,6 +6,7 @@ public class DistanceJoint3D : MonoBehaviour
{
public Rigidbody ConnectedRigidbody;
public float distance;
public float damper = 5f;
private Rigidbody rb;
......@@ -16,15 +17,21 @@ public class DistanceJoint3D : MonoBehaviour
private void Start()
{
distance = Vector3.Distance(rb.position, ConnectedRigidbody.position);
//distance = Vector3.Distance(rb.position, ConnectedRigidbody.position);
}
private void FixedUpdate()
{
var connection = rb.position - ConnectedRigidbody.position;
// var distanceDiscrepancy = distance - connection.magnitude;
var distanceDiscrepancy = distance - connection.magnitude;
rb.velocity = connection.normalized * Physics.gravity.y + Physics.gravity;
rb.position += distanceDiscrepancy * connection.normalized;
var velocityTarget = connection + rb.velocity;
var projectOnConnect = Vector3.Project(velocityTarget, connection);
rb.velocity = (velocityTarget - projectOnConnect) / (1 + damper * Time.fixedDeltaTime);
//rb.velocity = connection.normalized * Physics.gravity.y + Physics.gravity;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HookBehaviour : MonoBehaviour
{
[SerializeField]
private LineRenderer lr;
[SerializeField]
private Transform player;
[SerializeField]
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
gameObject.SetActive(false);
}
private void Update()
{
Debug.Log(rb.velocity);
lr.SetPosition(0, transform.position);
lr.SetPosition(1, player.position);
}
private void OnCollisionEnter(Collision collision)
{
rb.velocity = Vector3.zero;
rb.isKinematic = true;
}
public void EnableHook(Vector3 velocity)
{
lr.enabled = true;
lr.SetPosition(0, transform.position);
lr.SetPosition(1, player.position);
rb.isKinematic = false;
rb.velocity = velocity;
}
public void DisableHook()
{
lr.enabled = false;
gameObject.SetActive(false);
}
}
fileFormatVersion: 2
guid: 08eccdf623a41a9438f052a4d54491df
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -2,18 +2,66 @@
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
public float distance = 10.0f;
private CharacterController cc;
float velocityY = 0.0f;
const float gravity = 9.8f;
public HookBehaviour hook;
#if UNITY_EDITOR
private void OnDrawGizmos()
{
Gizmos.DrawLine(transform.position, (transform.position - Camera.main.transform.position) * 10);
}
#endif
private void Start()
{
cc = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
if(Input.GetKey(KeyCode.A)){
GetComponent<Rigidbody>().AddForce(-transform.right * 100);
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
if (Input.GetMouseButtonDown(1))
{
if (!hook.gameObject.activeSelf)
{
FireHook();
}
else
ReturnHook();
}
if (Input.GetButtonDown("Jump") && cc.isGrounded)
velocityY = 5.0f;
if (!cc.isGrounded)
velocityY -= gravity * Time.deltaTime;
cc.Move(horizontal * transform.right + vertical * transform.forward + new Vector3(0, velocityY, 0) * Time.deltaTime);
}
private void LateUpdate()
{
}
private void FireHook()
{
hook.gameObject.SetActive(true);
hook.transform.position = transform.position;
hook.EnableHook((transform.position - Camera.main.transform.position) * 50);
}
private void ReturnHook()
{
hook.DisableHook();
}
}
......@@ -3,7 +3,7 @@
--- !u!55 &1
PhysicsManager:
m_ObjectHideFlags: 0
serializedVersion: 11
serializedVersion: 13
m_Gravity: {x: 0, y: -9.81, z: 0}
m_DefaultMaterial: {fileID: 0}
m_BounceThreshold: 2
......@@ -17,11 +17,12 @@ PhysicsManager:
m_ClothInterCollisionDistance: 0
m_ClothInterCollisionStiffness: 0
m_ContactsGeneration: 1
m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
m_LayerCollisionMatrix: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdfffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
m_AutoSimulation: 1
m_AutoSyncTransforms: 0
m_ReuseCollisionCallbacks: 1
m_ClothInterCollisionSettingsToggle: 0
m_ClothGravity: {x: 0, y: -9.81, z: 0}
m_ContactPairsMode: 0
m_BroadphaseType: 0
m_WorldBounds:
......@@ -31,4 +32,4 @@ PhysicsManager:
m_FrictionType: 0
m_EnableEnhancedDeterminism: 0
m_EnableUnifiedHeightmaps: 1
m_DefaultMaxAngluarSpeed: 7
m_DefaultMaxAngularSpeed: 7
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment