- Home /
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:
As you note, a little stutter and wiggle left (is the weapon in your right hand?) is exactly what an active collider childed to a charController does.
I'd pause the scene when you know it's stuttering, and take another look for colliders (does the weapon have child colliders?)
Can you post a screen shot of the collider gizmos active ? Like @Owen Reynolds said, it probably has to do with colliders.
Set the weapon Collider as Trigger, and see if that makes a difference. If so, it definitely has to do with the Sword physics colliding the your players physics. Happens to me sometimes.
The weapon has a Box Collider by default, it gets deactivated when the player grabs it. The Box Collider is a trigger by default:
http://pn.dyna-studios.com/TA-Problemdemo/555.JPG
Here is the situation after it got parented to the character:
http://pn.dyna-studios.com/TA-Problemdemo/666.JPG
I totally removed the collider component for a test and continued gameplayback. The error is still there.
http://pn.dyna-studios.com/TA-Problemdemo/777.JPG
If it has to do something with the collider, then why does the character not slide to the right whe he is movig backwards? I mean it is the same animation, only played backwards and the same weapon that is in the same positions.
It is only stutterig when I move the character forward.
Answer by ThunderbirdX11 · Nov 02, 2012 at 07:53 PM
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 :)
Thx
Answer by Owen-Reynolds · Nov 02, 2012 at 02:00 PM
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.
I have also tried to leave some space between the collider and the weapon but it doesn't change anything. As soon as it gets parented, the stutter-effect is there again. Seems like it isn't a collider-issue. But what else could it be?
Bracket it. Try having the "weapon" be just an empty, and the code only child it. See if that causes it (probably won't.) Then add more until it stutters. Likewise, keep removing parts of the weapon and commenting out code until the error stops.
Your answer
Follow this Question
Related Questions
Attaching Weapon to Animated Character 0 Answers
Photon: Trembling Player 1 Answer
Custom Weapon Animation 2 Answers