Hi everyone, i’m new into Unity and C# Scripting and right now so i need some help, i also say beforehand [u]sorry if something is bad spelled because English is not my first language.[/u]
i’m trying to make a movement system for a 3D Player using Character Controller, using Gamesplusjames - Unity 3D Platformer Tutorial as a base (link to the playlist below):
After that and making a basic 3d movement i want to do more things over, but mainly an acceleration and deceleration system (after that things like go down steep slopes, or don’t instantly move the player changes direction(rotating the player first))
here is the code what i’m using right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
public float DistanceTerrain = 1.2f;
public float moveSpeed;
public float JumpForce;
public CharacterController Controller;
private Vector3 moveDir;
public float gravScale;
void Start()
{
Controller = GetComponent<CharacterController>();
}
void Update()
{
float yStore = moveDir.y;
// movement System
moveDir = (transform.forward * Input.GetAxisRaw("Vertical") * moveSpeed) + (transform.right * Input.GetAxisRaw("Horizontal") * moveSpeed);
moveDir = moveDir.normalized * moveSpeed;
moveDir.y = yStore;
//Jump System
if (isGrounded())
{
moveDir.y = 0;
if (Input.GetButtonDown("Jump"))
{
moveDir.y = JumpForce;
}
}
if (!Controller.isGrounded)
{
moveDir.y = moveDir.y + (Physics.gravity.y * gravScale * Time.deltaTime);
}
moveDir.y = moveDir.y + (Physics.gravity.y * gravScale *Time.deltaTime) ;
Controller.Move(moveDir*Time.deltaTime);
}
//JumpCheck
bool isGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, DistanceTerrain);
}
}
i can’t find in any part something to help me so i would really appreciate if someone can tell me how to do this or at least the other two ideas… any help is appreciated and welcome, and in advance thanks a lot.
One way is to modify your InputManager (In ProjectSettings) so that it only increases speed slower than it does now.
Another way is to track your current speed yourself, and only allow it to go up or down so much per frame. Right now you have one value for this (moveSpeed), but instead you would have another variable that would increase towards moveSpeed, or return back to zero when you hold or release a key.
As for moving up and down slopes, usually you take the normal of the ground and see if it is pointing behind you or pointing ahead of you, and either speed up or slow down your current move speed. Again you would have a temporary variable for this, while preserving moveSpeed’s originally-configured value.
This new value need to be attached directly with some Character Controller Value, like being the velocity? or i need to make the movement just a float what goes up per frame?
I imagine i need to change after that the “moveSpeed” value from “moveDir” to the new “currentSpeed” and so how i make the number stops when he reachs to the “moveSpeed” value? (thinking this is the maximum)
i added the currentSpeed 2 values what determines the speed of acceleration and deceleration and the nope value what check the last direction, having a correct deceleration[/code], idk if something here is unessesary or are better ways to do it.