Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by popuppirate · Oct 09, 2014 at 10:19 AM · transformpositionweaponjoint

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[i].GetComponent<Can_Pick_Up>();
             if (canpickupscript.can_pick_up==true){
                 Vector3 diff_vect = weapons [i].transform.position - transform.position;            
                 float diff_abs = Mathf.Abs (diff_vect.sqrMagnitude);                                
                 if (diff_abs < distance) {                                                            
                     closest_weapon = weapons [i];                                                    
                     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;
 }
 
 
     
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Baste · Oct 09, 2014 at 10:54 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image popuppirate · Oct 09, 2014 at 11:38 AM 0
Share

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.

avatar image popuppirate · Oct 10, 2014 at 06:25 AM 0
Share

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 see$$anonymous$$gly 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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Prefab teleporting on play mode ? 0 Answers

How can I Instantiate a prefab (projectile) consistently from the character? 0 Answers

Moving object with transform.position ignore other objects even if they collided 1 Answer

Need enemy to follow only active character 0 Answers

Drawing a (debug) line between anchored UI element and mouse? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges