- Home /
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);
}
}
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?
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
Follow this Question
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