Hi, I got a code from a friend in java and I need to convert it in c# but seems that nothing working and I didn’t find any help on web
here is parts of code :
Vector3 direction = position - transform.position;
...
Vector3 forward = transform.TransformDirection(Vector3.forward);
...
direction = forward * cm.maxForwardSpeed * speedModifier;
...
var directionVector = direction.forward;
now
on the last line Unity gives me this error :
Member 'UnityEngine.Vector3.forward.get' cannot be accessed with an instance reference; qualify it with a type name instead
what should I do and how should I change the script?
thanks
Well first there’s no var in C# and i have no idea what Vector3.forward.get is, so you should take that out i’m not sure what you’re doing because i don’t see Vector3.forward.get in the script
This is a common mistake, but the language you’re using is called JavaScript (aka UnityScript), not Java. (Java is a completely different language, and is not supported in Unity.)
Also, note that this:
Vector3 forward = transform.TransformDirection(Vector3.forward);
Can simply be written as ‘Vector3 forward = transform.forward’. (Or you can just use transform.forward directly.)
The reason this code:
var directionVector = direction.forward;
Doesn’t work is that you’re trying to access a static property through an object instance. In other words, a vector doesn’t have its own ‘forward’ vector.
I’m not sure what the intent is there though. Can you post the original UnityScript code?
ok guys here is the code :
void MoveTowards (Vector3 position )
{
var cm = GetComponent<CharacterMotor>();
Vector3 direction = position - transform.position;
direction=new Vector3 (direction .x, 0,direction .z);
if (direction.magnitude < 0.5) {
Stop ();
return;
}
// Rotate towards the target
var facingDirection = direction;
if (facingDirection.magnitude>1) facingDirection = facingDirection.normalized;
cm.desiredFacingDirection = facingDirection;
// Modify speed so we slow down when we are not facing the target
var forward = transform.TransformDirection(Vector3.forward);
var speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
// Move the character
direction = forward * cm.maxForwardSpeed * speedModifier;
var directionVector = direction .forward;
if (directionVector.magnitude>1) directionVector = directionVector.normalized;
cm.desiredMovementDirection = directionVector;
SendMessage("SetSpeed", cm.maxForwardSpeed * speedModifier, SendMessageOptions.DontRequireReceiver);
}
I should mention that the error I wrote was from vs.net compiler
but
unity says this error :
error CS0176: Static member `UnityEngine.Vector3.forward' cannot be accessed with an instance reference, qualify it with a type name instead
can this solve the problem or will it cause undesired effects ?
changing this :
var directionVector = direction.forward;
if (directionVector.magnitude>1) directionVector = directionVector.normalized;
cm.desiredMovementDirection = directionVector;
to this :
if (direction.magnitude > 1)
cm.desiredMovementDirection = direction.normalized;