Quantcast
Channel: Questions in topic: "improve"
Viewing all articles
Browse latest Browse all 12

Help Improve movement script

$
0
0
Hi guys there is anyway to improve this code? when the player move down on a surface that have a slope it doesn't touch the ground and make "little jump" that is unrealistic and kind of annoying. I gonna quote the whole code I have, if you have any suggestion is welcomed. /// /// Charactercontrollercs.cs /// Character Controller in CSharp v2.3 /// using UnityEngine; public class CharacterControllercs : MonoBehaviour { //Var definition public bool swimming = false; //Can be triggert to slow down the movements (like when u swim) public string moveStatus = "idle"; //movestatus for animations //Movement speeds private float jumpSpeed = 8.0f; //Jumpspeed / Jumpheight private float gravity = 20.0f; //Gravity for jump private float runSpeed = 10.0f; //Speed when the Character is running private float walkSpeed = 4.0f; //Speed when the Character is walking (normal movement) private float rotateSpeed = 250.0f; //Rotationspeed of the Character private float walkBackMod = 0.75f; //Speed in Percent for walk backwards and sidewalk //Internal vars to work with private float speedMod = 0.0f; //temp Var for Speedcalculation private bool grounded = false; //temp var if the character is grounded private Vector3 moveDirection = Vector3.zero; //move direction of the Character private bool isWalking = false; //toggle var between move and run private bool jumping = false; //temp var for jumping private bool mouseSideButton = false; //temp var for mouse side buttons private float pbuffer = 0.0f; //Cooldownpuffer for SideButtons private float coolDown = 0.5f; //Cooldowntime for SideButtons private CharacterController controller; //CharacterController for movement //Every Frame void Update () { //Set idel animation moveStatus = "idle"; isWalking = true; // Hold "Run" to run if(Input.GetAxis("Run") != 0) isWalking = false; // Only allow movement and jumps while grounded if(grounded) { //movedirection moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical")); //pushbuffer to avoid on/off flipping if(pbuffer>0) pbuffer -=Time.deltaTime; if(pbuffer 1) moveDirection.z = 1; //Strafing move (like Q/E movement moveDirection.x -= Input.GetAxis("Strafing"); // if moving forward and to the side at the same time, compensate for distance if(Input.GetMouseButton(1) && (Input.GetAxis("Horizontal") != 0) && (Input.GetAxis("Vertical") != 0)) { moveDirection *= 0.7f; } //Speedmodification / is moving forward or side/backward speedMod = ((Input.GetAxis("Vertical") 0) moveStatus = isWalking ? "walking" : "running"; if(moveDirection.z 0) moveStatus = isWalking ? "sidewalking_r" : "siderunning_r"; if(moveDirection.x 0) moveStatus = isWalking ? "swim" : "swimfast"; if(moveDirection.z 0) moveStatus = isWalking ? "sideswim_r" : "sideswimfast_r"; if(moveDirection.x (); //Move Charactercontroller and check if grounded grounded = ((controller.Move(moveDirection * Time.deltaTime)) & CollisionFlags.Below) != 0; //Reset jumping after landing jumping = grounded ? false : jumping; //movestatus jump/swimup (for animations) if(jumping) moveStatus = "jump"; if(jumping && swimming) moveStatus = "swimup"; } } Is not my code I took from [FCC][1]. It work exactly how I wonted. But i can't make it work better with the ground. I'm workin on animation, but I'm not going to attach that part of the code. [1]: https://ruhrnuklear.de/fcc/

Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles





Latest Images