The question is answered, right answer was accepted
Reference Vector 3 variable to float variable.
Hello everyone !
I have a problem and it's killing me a little inside each time I can't fix it. My main goal is to have var XP and var size communicating with each other. I need var XP to feed numbers to var size. Please help me out with this !
#pragma strict
public var XP : float; // This variable will feed numbers to var size.
public var randomRangeXP : float;
public var size : Vector3; // This variable will take the numbers from var XP.
function Update ()
{
XP += randomRangeXP = XP ;
// I need a line that will take the number from var XP and feed it to var size.
}
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.tag == "Bacteria")
Destroy(other.gameObject);
// Once I have that line that will feed var size, I'll have another line here that will make my character bigger depending on the XP given.
}
I hope you guys understand what I want to do, I tried to be as clear as possible, I'm new to code so please be gentle ! Thank you all for your time !
I'm trying to figure out what you are doing, but it is really hard with the broken code formatting. Could you please edit the question to put all the code inside a code block? please :)
Answer by Socapex · Dec 31, 2015 at 07:37 PM
Wild guess: You want to increment the size using XP and a little bit of randomness. I will write C#.
// Increase size using +- 2 of the XP
float incrementSize = Random.Range(XP - 2, XP +2);
size.x += incrementSize;
size.y += incrementSize;
size.z += incrementSize;
// Increase size by a fixed amount, the quantity of XP. XP == size
size.x = XP;
size.y = XP;
size.z = XP;
Close?
Sorry for the mess. I'm new to code and I'm still learning.
I went ahead and revised it again and made it neater.
I don't need randomness, that part I've already taken care of.
What I really need is to be able to give variable size the value from variable XP. But because variable size is a Vector3, it's not communicating with variable XP.
Then the second part should do it, you can +=, -=, /=, *= and = the value. You can work on the individual parts of the vector (x,y,z) with floats. Think of a vector3 as a fancy array of float[3]
By communicating I am guessing you mean transform the vector3 in some way.
I have no idea how to translate your response to Javasript lol. $$anonymous$$aybe something I should have mentioned is "I'm working with Javascript".
Answer by Warhulk1745 · Dec 31, 2015 at 09:04 PM
#pragma strict
public var XP : float;
public var randomRangeXP : float;
public var size : Vector3;
function Start () {
randomRangeXP = Random.Range (5.00,50.00);
}
function Update () {
XP == size ;
size.x =XP;
size.y = XP;
size.z = XP;
}
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.tag == "Bacteria")
Destroy(other.gameObject);
transform.localScale += size ;
XP += randomRangeXP;
// transform.localScale += new Vector3(20,20,20);
}
So, part of my problem was that I had two scripts and I wasn't accessing variables from my other script the right way. So what I ended up doing was putting everything in one single script. The code I'm using is allowing the two variables to communicate how I needed them to. Thanks Socapex !
Follow this Question
Related Questions
Why are my Vector3s returning inaccurate floats? I don't think it's the debug .ToString() issue. 2 Answers
Cannot convert float to int? 1 Answer
Non physics determinism of floating point math across platforms 1 Answer
The moving average (filter) of the acquired value of the HTC vive controller 0 Answers
float.Parse changing dot position 1 Answer