I’ve been working on a game for nearly a week and, being a near-complete noob with lots of programming experience in anything but c#, I’m stuck while making a movement script.
This is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public CharacterController controller;
public Vector3 MovementSpeed;
public float MouseSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
#region BasicMovement
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if (Input.GetKeyDown(KeyCode.W)) MovementSpeed.z += 4;
if (Input.GetKeyDown(KeyCode.A)) MovementSpeed.x -= 4;
if (Input.GetKeyDown(KeyCode.S)) MovementSpeed.z -= 4;
if (Input.GetKeyDown(KeyCode.D)) MovementSpeed.x += 4;
if (Input.GetKeyUp(KeyCode.W)) MovementSpeed.z -= 4;
if (Input.GetKeyUp(KeyCode.A)) MovementSpeed.x += 4;
if (Input.GetKeyUp(KeyCode.S)) MovementSpeed.z += 4;
if (Input.GetKeyUp(KeyCode.D)) MovementSpeed.x -= 4;
}
else
{
if (Input.GetKeyDown(KeyCode.W)) MovementSpeed.z += 2;
if (Input.GetKeyDown(KeyCode.A)) MovementSpeed.x -= 2;
if (Input.GetKeyDown(KeyCode.S)) MovementSpeed.z -= 2;
if (Input.GetKeyDown(KeyCode.D)) MovementSpeed.x += 2;
if (Input.GetKeyUp(KeyCode.W)) MovementSpeed.z -= 2;
if (Input.GetKeyUp(KeyCode.A)) MovementSpeed.x += 2;
if (Input.GetKeyUp(KeyCode.S)) MovementSpeed.z += 2;
if (Input.GetKeyUp(KeyCode.D)) MovementSpeed.x -= 2;
}
controller.SimpleMove(MovementSpeed);
#endregion
#region Rotation
if (Input.GetAxis("Mouse X") > 0) transform.Rotate((Vector3.up) * MouseSpeed);
if (Input.GetAxis("Mouse X") < 0) transform.Rotate((Vector3.up) * -MouseSpeed);
#endregion
}
}
As shown in the code, I’m using a character controller instead of a rigidbody and missing an up/down look script.
The code currently uses the old input system, and I’m hoping to update it to the new system once this movement script is complete, as all I have to do is change the input methods.
P.S. I posted this in help room as well and since I posted it there I updated my move script to the new input system but I’m still stuck and could really do with some help with the actual movement.