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
0
Question by popuppirate · Oct 06, 2014 at 11:09 PM · transformfloatrayvector3 operations

Distance Variable Not Returning Value/Ray not casting

Hi guys,

Been working on a script to find the nearest object named 'Weapon' and pull it towards the player when a bool becomes true- I'll develop this further once I've got it working, but at the moment I'm debugging with a raycast between the two objects.

However, for some reason when I set the bool 'attach_weapon' to true, my function for dealing with the location of the objects doesn't work and a ray isn't cast. I've checked and the public float distance_local doesn't come back with anything.

My maths tells me subtracting the final vector from the initial vector gives the direction between objects and that I can calculate this as a distance as I have in the other part of the script using Mathf.Abs(MyVector.sqrtMagnitude). Here is my script so far.

Thank you for your time!

Popuppirate

 using UnityEngine;
 using System.Collections;
 
 public class Weapon_Control_Script : MonoBehaviour {
 
     public GameObject closest_weapon;
     public bool attach_weapon;
     public float distance;
     public float distance_local;
      
 
 
     // Use this for initialization
     void Start () {
     attach_weapon = false;
     }
 
     
     // Update is called once per frame
     void Update () {
         if (attach_weapon == false) {
             SearchForWeapons();        
         }
 
         if (attach_weapon == true) {
             AttachWeapon();    
         }
 
 }
 
 
 void AttachWeapon(){ //Look Here!
 
 
         Vector3 player_vect = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
         Vector3 weapon_vect = new Vector3 (closest_weapon.transform.position.x,closest_weapon.transform.position.y,closest_weapon.transform.position.z);
         Vector3 direction = (weapon_vect - player_vect);
         float distance_local = Mathf.Abs (direction.sqrMagnitude);
         Debug.DrawRay (player_vect, direction*distance_local, Color.green,1f);
         //closest_weapon.rigidbody.AddForce (-direction * 4);
 }
 
 
 void SearchForWeapons(){ //This works lovely jubly
     GameObject[] 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;                                                            
                 }
             }
             
         }
         Debug.Log (closest_weapon.name);
 }
 
 
     
 }
 
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
0

Answer by Habitablaba · Oct 07, 2014 at 12:29 AM

Is there a reason you are using sqrMagnitude instead of just magnitude?
Does closest_weapon get set like you expect it to?
Is this script attached to the player?

Comment
Add comment · Show 1 · 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 14, 2014 at 06:21 AM 0
Share

Sorry about the wait for a reply, Habitablaba! I've actually since asking this completed this script using a spherecast from the player to obviate the need for a single Ray. I shall post the final code when I'm on a computer with it on!

However, I have been (unsuccessfully) experimenting with casting a constant beam (line) between a point on the player_attach_point and the weapon_attach_point when it's within 180 degrees of the player_attach_point that I can stick a particle effect on. This involves raycasting and I shall probably ask it as a different question.

Cheers anyway!

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

Problems with a quad dimensions 0 Answers

Accurate placed object's transform messes up on game start 1 Answer

Bring ray to gameObject.y position when mouse cursor is on a different point 1 Answer

How do I smooth out my crouch camera movement 0 Answers

What the hell? Cannot convert float to int PROBLEM. 2 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