Keeping an Object Locked In Position Around Another

Hi Guys,

I’m making a script in which objects defined as weapons can be dragged to the player and placed in position using physics. This is achieved using two empty objects, Player_Attach_Point and Weapon_Attach_Point, as reference points to move the weapon to a desired location after it hits a SphereCast. However, I wanted to set the player’s weapon to remain in a constant position upon rotation and movement. I tried using a FixedJoint between the player and the weapon, but this caused the camera and player to go nuts (rotating really, really fast etc.)

Can anyone suggest a reasonable way of making the weapon stay in place indefinitely?

Cheers,

Popuppirate

using UnityEngine;
using System.Collections;

public class Weapon_Control_Script : MonoBehaviour {

	public GameObject closest_weapon;
	public bool attach_weapon;
	public float distance;

	private GameObject player;
	private Transform p_trans;
	private Transform w_trans;
	private GameObject[] weapons;
	private FixedJoint temp_joint;
	private Vector3 player_vect;
	private Vector3 weapon_vect;

	// Use this for initialization
	void Start () {
	p_trans = transform.FindChild("PlayerAttachPoint");
	attach_weapon = false;
	}

	
	// Update is called once per frame
	void Update () {
		if (attach_weapon == false) {
			SearchForWeapons();		

		}

		if (distance < 100f && Input.GetButtonDown ("Fire1")) {
			attach_weapon=true;		
		}

		if (attach_weapon == true) {
			MoveWeapon();

		if(Input.GetButtonDown ("Fire2")){
			attach_weapon=false;
			}
		}

}

void AttachWeapon(){ //This creates a joint between weapon and player, doesn't work as expected 
		temp_joint=collider.gameObject.AddComponent<FixedJoint>();
		temp_joint.anchor=player_vect;
		temp_joint.axis=player_vect+new Vector3(0.55f,0f,0f);
		temp_joint.connectedBody=closest_weapon.rigidbody;
		closest_weapon.transform.parent=gameObject.transform;
		}
	
	

void MoveWeapon(){ //This Moves Weapon
		closest_weapon.rigidbody.useGravity=false;
		RaycastHit hit;
		player_vect = p_trans.position;
		weapon_vect = w_trans.position;

		Vector3 direction = (weapon_vect - player_vect);
		float distance_local = Mathf.Abs (direction.sqrMagnitude);
		closest_weapon.rigidbody.AddForce (-direction * distance_local);
		bool cast = Physics.SphereCast (transform.position, 2f, transform.forward, out hit, Mathf.Infinity);
		if (cast==true && hit.collider.gameObject.name==closest_weapon.name){ {

				closest_weapon.rigidbody.velocity=rigidbody.velocity;
				closest_weapon.rigidbody.angularVelocity=rigidbody.angularVelocity;
				//closest_weapon.transform.position=Vector3.MoveTowards(weapon_vect, player_vect, 2*Time.deltaTime);
				closest_weapon.transform.localPosition= player_vect+new Vector3(0.25f,0f,0f);
				closest_weapon.transform.rotation=transform.rotation;
			}
		}
		Debug.Log (player_vect + "," + direction);
		}




void SearchForWeapons(){ //This finds nearest weapon

	weapons = GameObject.FindGameObjectsWithTag ("Weapon"); 
	distance = Mathf.Infinity;															
	for (int i=0; i<weapons.Length; i++) {
		Can_Pick_Up canpickupscript=weapons*.GetComponent<Can_Pick_Up>();*
  •  	if (canpickupscript.can_pick_up==true){*
    

Vector3 diff_vect = weapons *.transform.position - transform.position;
float diff_abs = Mathf.Abs (diff_vect.sqrMagnitude);
if (diff_abs < distance) { *

closest_weapon = weapons ;
distance = diff_abs;

* }*
* }*

* if (w_trans==null||w_trans.name!=closest_weapon.name){*

* w_trans=closest_weapon.transform.FindChild(“WeaponAttachPoint”);*

* }*

* }*
* Debug.Log (closest_weapon.name);
closest_weapon.rigidbody.useGravity=true;
_}*_

}

1 Answer

1

If you want the weapon to have a constant position and rotation in relation to the player, set it’s transform as a child of the player transform, and set the weapon’s rigidbody as kinematic.

Then it will rotate along with the player, and have a fixed offset from the player’s body.

Cheers Baste-shall give this a go later! I did at first try setting the actual weapon object to be a child of the player(didn't work), but I didn't consider setting the transform as the child.

This works excellently. However, there's a regular bug I encountered where the object moves in place, but then upon moving the player character the weapon is attached to a given distance the weapon suddenly disappears very high in the air at a seemingly random vector from the player. I get the feeling it might be a collisions-based issue, but I'm not sure, Any advice would help! Thanks for your help, Baste!