First time posting on the site, been lurking for a long time. I’ve been following along these tutorials online, learning here and there and for some reason when I tried this script it just doesn’t work as intended. It’s suppose to be a player movement script from (ETeeskiTutorials) I took a look at his forum but people are having other problems none like mines. So I figured maybe I should reach out to the rest of the community and see if you folks could help me out here.
So below is the script, only minor changes were made to suit my player movement a little better but now the capsule just jumps in place and doesnt want to move, any ideas? Thanks in advance btw.
NOTE: I am using the Free version of Unity in case anyone wanted to know, does that affect certain scripting?
var walkAcceleration : float = 5;
var walkAccelAirRacio : float = 0.1;
var walkDeacceleration : float = 5;
@HideInInspector
var walkDeaccelerationVolx : float;
@HideInInspector
var walkDeaccelerationVolz : float;
var cameraObject : GameObject;
var maxWalkSpeed : float = 20;
@HideInInspector
var horizontalMovement : Vector2;
var jumpVelocity : float = 20;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;
function Update ()
{
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
if (Input.GetAxis("Horizontal") == 0 Input.GetAxis("Vertical") == 0 grounded){
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration); }
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
if (Input.GetButtonDown("Jump") grounded)
rigidbody.AddForce(0,jumpVelocity,0);
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
function OnCollisionExit ()
{
grounded = false;
}
Well, as I am reading it, you only can move when you have no input and are grounded:
if (Input.GetAxis("Horizontal") == 0 Input.GetAxis("Vertical") == 0 grounded){
}
Inside that, is everything relative to moving.
That being said, I took some time to clean up the theory of how you are doing this.
#pragma strict
var maxWalkSpeed : float = 20;
var walkAcceleration : float = 5;
var walkAccelAirRacio : float = 0.1;
var walkDecelleration : float = 5;
var jumpVelocity : float = 20;
var grounded : boolean = false;
function Update (){
var view : Vector3 = -transform.InverseTransformPoint(Camera.main.transform.position);
view.y = 0;
transform.forward = view;
var movement : Vector3 = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
if(movement.magnitude > maxWalkSpeed) movement = movement.normalized * maxWalkSpeed;
var input : Vector3 = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
var magnitude : float = input.magnitude;
if(grounded){
if(magnitude == 0){
movement = Vector3.MoveTowards(movement, Vector3.zero, walkDecelleration * Time.deltaTime);
}
if(Input.GetButtonDown("Jump")){
grounded = false;
rigidbody.AddForce(0,jumpVelocity,0);
}
} else {
input *= walkAccelAirRacio;
}
if(magnitude > 0) rigidbody.AddRelativeForce(input * walkAcceleration);
// created for a standard capsule.
var ray : Ray = new Ray(transform.position, Vector3.down);
var hit : RaycastHit;
grounded = Physics.SphereCast(ray, 0.5f, 1.15f);
}
I made sure that you will slow when grounded, but not using a dampened deceleration. I think that was a bit much. I kept a bit of your original code. I dumped the camera stuff to generate rotation for a very simple mathematical way.
I did a bit of other stuff, but looking at the time. I dont have enough to explain it. Check it over, and play with it. It is similar to your original tutorial, but different enough to give a different point of view.
Hey, thanks for responding, the script works and I can definitely move around now but there is a slight problem, my camera isnt synced like before with the capsule, when I move forward for example and look left or right, instead of moving in that general direction the player continues straight isnt stopping like I previously had it setup, kind of just slides for a while. Is there something you added that I need to add to my MouseLookScript? I’ll drop that down below if that is the case.
var defaultCameraAngle : float = 60;
@HideInInspector
var currentTargetCameraAngle : float = 60;
@HideInInspector
var racioZoom : float = 1;
@HideInInspector
var racioZoomV : float;
var racioZoomSpeed : float = 0.2;
var lookSensitivity : float = 5;
@HideInInspector
var yRotation : float;
@HideInInspector
var xRotation : float;
@HideInInspector
var currentYRotation : float;
@HideInInspector
var currentXRotation : float;
@HideInInspector
var yRotationV : float;
@HideInInspector
var xRotationV : float;
var lookSmoothDamp : float = 0.1;
@HideInInspector
var currentAimRacio : float = 1;
function Update ()
{
if (currentAimRacio == 1)
racioZoom = Mathf.SmoothDamp(racioZoom, 1, racioZoomV, racioZoomSpeed);
else
racioZoom = Mathf.SmoothDamp(racioZoom, 0, racioZoomV, racioZoomSpeed);
camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
xRotation = Mathf.Clamp(xRotation, -90, 90);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
The weird thing is the camera now moves like a Free Look camera, I initially had it set up for an FPS camera. Can’t seem to get it to work the original way. Script works fine just the camera isn’t like before and kind of throw a wrench in everything. Going to have to back track and see where I went wrong. Thanks again for the help.