Hi there. I am new to gamedev stuff, just learning things, and I need some help with controls.
The idea is make my character move by WASD, and look at the cursor position. I done with camera, but can’t make it follow the cursor to the certain limit. Here’s a code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour
{
//Variables
public Transform player;
public float smooth = 0.3f;
public float height;
private Vector3 velocity = Vector3.zero;
//Methods
void Update()
{
Vector3 pos = new Vector3();
pos.x = player.position.x;
pos.z = player.position.z - 7f;
pos.y = player.position.y + height;
transform.position = Vector3.SmoothDamp(transform.position, pos, ref velocity, smooth);
}
}
The second one is make player moving. I got a script from tutorial, but the problem is cursor position affects my movement, here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//Variables
public float movementSpeed;
public GameObject camera;
//Methods
void Update()
{
//Player facing mouse
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDist = 0.0f;
if(playerPlane.Raycast(ray, out hitDist))
{
Vector3 targetPoint = ray.GetPoint(hitDist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
targetRotation.x = 0;
targetRotation.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 7f * Time.deltaTime);
}
//Player Movement
if (Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
transform.Translate(Vector3.back * movementSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
transform.Translate(Vector3.left * movementSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
}
I never worked with programming, so it’s a kinda hard for me for now. Any help will be appreciated.