Character movement distorted when holding weapon

Hello everyone,

Here is my problem:

I have a player and a weapon. If the player walks or runs around without the weapon, he is able to run normal.

If the weapon is attached to the player, he walks a bit left with every animation loop and stutters when running with the weapon, but walking backwards (with S-Key) is possible without a distortion. It’s pretty weird…

You can see it for yourself here: Webplayer

Try to run or walk directly to a destination without and then with the weapon

Controls:

  • Walk = W
  • Run = W + R
  • Walk backwards = S

The weapon has a box collider component, wich is controlled by the item_pickup script and switched off when the weapon gets parented.

Item-pickup is written to parent the weapon to the weaponmount-Empty in the right hand and to disable the weapons box collider component.

What I tried so far:

  • I can only prove the colliders innocence for this behaviour. It gets disabled when you grab the weapon, but the stutters are still there.

Here is the item_pickup script (attached to the player)

var clone : GameObject;
var Player : GameObject;

var item_name;
var WeaponHand : GameObject;

var showGUI = false; 

WeaponHand = GameObject.FindWithTag("weaponmount"); //The Tag of The Empty that's parented to the right Hand
Player = GameObject.FindWithTag("Player");

function OnTriggerStay (other : Collider) {
    if (other.gameObject.tag == "weapon"){
    	
    	showGUI = true;	
    	item_name = other.gameObject.name;
    	
    	if (Input.GetKeyUp ("e")){
    	
    		clone = GameObject.Find(item_name);  //Find name of the Item we are colliding with
    	
    		clone.transform.parent = WeaponHand.transform;
    		clone.transform.position = WeaponHand.transform.position;
    		clone.transform.rotation = WeaponHand.transform.rotation;
    		clone.collider.enabled = false;
    		showGUI = false;
    		Debug.Log("Parented !");    	
    		}
    	
    	}
    }	
    	function OnGUI(){
		    if (showGUI){
		    	GUI.Label (Rect (500,500,1000,1000), "[E] Waffe aufnehmen"); //"Press E to Pickup the Weapon"
		    }
    	   
    	  }

Here is also my walk-script (also attached to the player):

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(CharacterController))]

public class walk : MonoBehaviour {
	public float moveSpeed = 8;
	public float Strafespeed = 2.5f;
	public float rotateSpeed = 250;
	public float runspeed = 16;

	private Transform _myTransform;
	private CharacterController _controller;
	
			
	public void Awake(){
		_myTransform = transform;
		_controller = GetComponent<CharacterController>();
	}
	
	// Use this for initialization
	void Start () {
	animation.wrapMode = WrapMode.Loop;

	}
	
	// Update is called once per frame
	void Update () {
		DontDestroyOnLoad (this);
		Turn();
		move();
		attack();
}
	
	private void Turn(){
				if(Mathf.Abs(Input.GetAxis("Rotate Player")) > 0){
			_myTransform.Rotate(0, Input.GetAxis("Rotate Player") * Time.deltaTime * rotateSpeed, 0);
	}
}
	
		private void move(){
		
			
			if(Mathf.Abs(Input.GetAxis("Move Forward")) > 0){
	
				animation.CrossFade("walk", 0.2f);
				_controller.SimpleMove(_myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Move Forward") * moveSpeed);
			
			if(Mathf.Abs(Input.GetAxis("run")) > 0 && Mathf.Abs(Input.GetAxis("Move Forward")) > 0){
								
								animation.CrossFade("run", 0.2f);
								_controller.SimpleMove(_myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("run") * runspeed);
						}
			}
			
			if(!(Input.anyKey)){
					
				animation.CrossFade("idle", 0.2f);
				
			}	
	}
	private void attack(){
				
						if(Input.GetMouseButtonDown(0)){
								//Debug.Log ("Klick");
								animation.wrapMode = WrapMode.Once;
								animation.CrossFade("attack2", 0.2f);
								return;
			
								
						}
			
				}
	
	

	}

Here are the properties of my player character:

alt text

For a test, try shrinking the gun collider away from the part the player grabs, so there’s a wider gap between it and the player while holding it. If that changes nothing, it wasn’t the collider.

Some explanation: char controllers only “own” their built-in capsules. If they have other colliders childed to them, unlike regular physics, they don’t know that they own them. When charCons move, they treat child colliders as solid, immovable objects, same as all other rigid bodies (they can’t even push them, since they never push anything.)

After the char controller is done smashing and sliding against a child “obstacle,” parenting moves the child. To the char controller, it looks like some idiot keeps moving a wall in front of it.

Moving backwards, even if the gun animation clips it a little, the char is moving away from that obstacle, into open space, so isn’t blocked.

OK I found the reason:

After I created an empty object and parented it as a “weapon” with no strange behavior afterwards, I came to think the weapon mesh is the reason for the issue. Although I deactivated “create mesh colliders” in the .fbx-Import, the childobject of the imported-Object had a mesh-collider component activated.

I removed it and now everything works fine :slight_smile:

Thx