- Home /
Player control problems with gun script
I'm following a tutorial on how to make a fps game and i got all the movement stuff down but when it comes to the gun script part it messed everything up. In this video he can move around with the gun but when I copied everything he's done I can't move at all, the player capsule runs around but I'm stuck and can't move but i can look around and my gun is stuck too and it flashes and glitches out. I'll provide a youtube link to the part where I am at and stuck.
When I finish the the tutorial to the end of the video, I can get the gun to move around with the mouse look, but moving with W,A,S,D keys just makes the player capsule move around and not me, Im just stuck in a fixed position.
Can you post the code that reacts to key presses from W, A, S, and D? Are you restricting the movement of the camera in some way?
This problem only started to happen after I added the gun script. I noticed that when I change the value of the hold height of the gun it changes the position of the camera too which is weird. $$anonymous$$aybe the gun has a collider mesh enabled that comes from Blender? I tried disabling it with a script and it allows me to move again but the gun stays anchored and doesn't move at all.
here is my player movement script
var walkAccelRacio : float = 0.1;
var walkDeacceleration : float = 5;
@HideInInspector
var walkDeaccelerationVolx : float;
@HideInInspector
var walkDeaccelerationVolz : float;
var cameraObject : GameObject;
var maxWalkSpeed : float = 20;
@HideInInspector
var horizontal$$anonymous$$ovement : Vector2;
var jumpVelocity : float = 20;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;
function Update ()
{
horizontal$$anonymous$$ovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontal$$anonymous$$ovement.magnitude > maxWalkSpeed)
{
horizontal$$anonymous$$ovement = horizontal$$anonymous$$ovement.normalized;
horizontal$$anonymous$$ovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontal$$anonymous$$ovement.x;
rigidbody.velocity.z = horizontal$$anonymous$$ovement.y;
if (grounded){
rigidbody.velocity.x = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
rigidbody.velocity.z = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent($$anonymous$$ouseLookScript).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 * walkAccelRacio * Time.deltaTime, 0, Input.GetAxis("Vertical")* walkAcceleration * walkAccelRacio* Time.deltaTime);
if (Input.GetButton("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;
}