what am i doing wrong

S is just walking back and LeftShift is Running, Im trying to make it so if you walk back you cant sprint
No description
11 Replies
ero
ero2w ago
Check if the user is pressing S in whatever method it's controlling your sprint? And then don't run the sprint logic?
G̵̈́͑L̶̈̕1̶̌̀TC̴̑HY
im pretty new to coding so would you mind could maybe help tell me what to put?
ero
ero2w ago
No, for I didn't have enough context to do that
G̵̈́͑L̶̈̕1̶̌̀TC̴̑HY
do you want the whole code?
ero
ero2w ago
The parts that are relevant, yes
G̵̈́͑L̶̈̕1̶̌̀TC̴̑HY
well heres the code using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(CharacterController))] public class PlayerMovement : MonoBehaviour { public Camera playerCamera; public float walkSpeed = 6f; public float runSpeed = 12f; public float jumpPower = 7f; public float gravity = 10f; public float lookSpeed = 2f; public float lookXLimit = 45f; public float defaultHeight = 2f; public float crouchHeight = 1f; public float crouchSpeed = 3f; private Vector3 moveDirection = Vector3.zero; private float rotationX = 0; private CharacterController characterController; private bool canMove = true; void Start() { characterController = GetComponent<CharacterController>(); Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } void Update() { Vector3 forward = transform.TransformDirection(Vector3.forward); Vector3 right = transform.TransformDirection(Vector3.right); bool isRunning = Input.GetKey(KeyCode.LeftShift); float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0; float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0; float movementDirectionY = moveDirection.y; moveDirection = (forward * curSpeedX) + (right * curSpeedY); { } if (Input.GetButton("Jump") && canMove && characterController.isGrounded) { moveDirection.y = jumpPower; } else { moveDirection.y = movementDirectionY; } if (!characterController.isGrounded) { moveDirection.y -= gravity * Time.deltaTime; } if (Input.GetKey(KeyCode.C) && canMove) { characterController.height = crouchHeight; walkSpeed = crouchSpeed; runSpeed = crouchSpeed; } else { characterController.height = defaultHeight; walkSpeed = 6f; runSpeed = 12f; } characterController.Move(moveDirection * Time.deltaTime); if (canMove) { rotationX += -Input.GetAxis("Mouse Y") * lookSpeed; rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit); playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0); transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0); } } }
ero
ero2w ago
So just add a check for if S is pressed in your isRunning check
bool isRunning = Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.S);
bool isRunning = Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.S);
FusedQyou
FusedQyou2w ago
@G̵̈́͑L̶̈̕1̶̌̀TC̴̑HY You need to configure your editor to support Unity. Right now you have literally 0 intellisense for anything Unity related
FusedQyou
FusedQyou2w ago
Install & configure Visual Studio Tools for Unity
Connect Unity and Visual Studio and use Visual Studio Tools for Unity to support writing and debugging for cross-platform development.
FusedQyou
FusedQyou2w ago
Until you configured the editor, please do not continue developing at all because there are multiple things that simply do no work in your case
Want results from more Discord servers?
Add your server