- Home /
Question by
KOR_APUcard · Nov 07, 2018 at 02:04 PM ·
c#movementcrouchsprint
How to make character movement like a Half-Life 2?
I making some projects...
I'm making a project (a large scale), but I still have a lot of inexperienced steps to learn programming. So I always have some problems. One of them is this:
How to make character movement like a Half-Life 2 using C#?
Once I wrote the Character Movement. But something seems to be a problem. Some of the added features will not work.
Below are the scripts I wrote.
Character Movement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class script_playerMovement_Ctrl : MonoBehaviour
{
private Vector3 moveDir = Vector3.zero;
private float gravity = 10.0f;
private float speed = 4.0f;
private float walkSpeed = 1.5f;
private float runSpeed = 6.0f;
private float crchSpeed = 2.0f;
private float jumpForce = 4.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDir = transform.TransformDirection(moveDir);
moveDir *= speed;
if (Input.GetButtonDown("Jump"))
{
moveDir.y = jumpForce;
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
// Character Movement Action: Crouch
if (Input.GetKey(KeyCode.LeftControl))
{
controller.height = 0.8f;
speed = crchSpeed;
}
else
{
controller.height = 1.8f;
speed = 4.0f;
}
// Character Movement Action: Sprint
if (Input.GetKey(KeyCode.LeftShift))
{
speed = runSpeed;
}
else
{
speed = 4.0f;
}
// Character Movement Action: Walk
if (Input.GetKey(KeyCode.LeftAlt))
{
speed = walkSpeed;
}
else
{
speed = 4.0f;
}
}
}
What's wrong? I can not feel it..
Comment
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How to make a good PlayerMovement 1 Answer
Custom C# Crouch Script 1 Answer
Multiple Cars not working 1 Answer
My character won't move in C#. 1 Answer