- Home /
Duplicate Question
Vector3 Issues - Not Able To Solve Constructor issue
Alright, I've done my research into this issue, examined similar scripts and tested it, but I've hit an error with the Vector3 doesn't have a visible constructor. I found information that this is simply because I need to construct, that is, specify the property.
I believe I've hit a property I cannot specify, for some reason. I'm sure the error may be in the property lookDir (look direction), but no matter how much tampering, I cannot get Vector3 to accept it. The error specifically is BCE0024: The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(UnityEngine.Vector3)'
with conflict with the line: myTransform.rigidbody.velocity = Vector3(lookDir.normalized MoveSpeed Time.deltaTime);
The script works perfectly if lookDir is replaced with transform.forward (but doesn't work fine in-game), so lookDir is certainly the issue.
If someone can offer advice on how to address the issue, and if kindly enough, offer a correction entirely, it would be much appreciated.
The actual script is below:
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;
var myTransform : Transform;
var distance = Vector3.Distance(Player.position, transform.position);
function Awake(){
myTransform = transform;
}
function Update ()
{
if (distance <2f)
{
var dist = Vector3.Distance(Player.position, myTransform.position);
var lookDir = Player.position - myTransform.position;
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
myTransform.rigidbody.velocity = Vector3(lookDir.normalized * MoveSpeed * Time.deltaTime);
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
}
}
}
}
this is a duplicate question so I have deleted it. please do NOT post duplicates, to avoid wasting the time of the many dedicated board members! thanks
Answer by Benproductions1 · Apr 06, 2013 at 10:58 AM
Hi! there,
Your problem is that your trying to create a Vector3
from a Vector3
. As the debug message tells us, none of the constructor for Vector3
take a Vector3
as an argument.
The cool thing is you don't need it at all! Any calculation with a Vector3
that is legal, automatically yields a Vector3, so:
Vector3 * float = Vector3
Vector3 / float = Vector3
Therefore your line should be:
myTransform.rigidbody.velocity = lookDir.normalized * MoveSpeed * Time.deltaTime;
There is no need to tell unity the outcome of a calculation, it's all built in :)
Hope this helps, Benproductions1