- Home /
Operator cannot be used with left hand side of type....
I'm having a little brain racking problem, Basically i'm trying to determine the future position of an enemy object, now I think I have the code right in principle, however the Unity console is throwing up an error that I can't seem to fix or have no idea of where to start.
Here is the code section: -
if (Vector3.Distance(Target.position, transform.position) <= RadarRange || Vector3.Angle(Target.position, transform.position) < 10)
{
var EnemySpeed = Target.GetComponent(FriendlyAI).InterceptSpeed;
var TargetFuturePosition : Vector3 = Target.position + EnemySpeed;
var FutureDirection : Vector3 = TargetFuturePosition - transform.position;
transform.Translate (0, 0, InterceptSpeed * Time.deltaTime);
var lookFutureRotation = Quaternion.LookRotation (FutureDirection);
transform.rotation = Quaternion.Slerp(rotation, lookFutureRotation, TurnSpeed * Time.deltaTime);
transform.Rotate (0,0, -angle * Roll);
IsIntercepting = true;
IsPatrolling = false;
IsEvading = false;
IsAttacking = false;
}
The problem is in the FutureTargetDirection variable, the Unity console is saying that I cannot use the operator + with the left hand side of type Vector 3 and the right hand side of float.
I don't want to go down the road of rigidbodies since i've tried using them and they didn't like how they acted in my game, so I decided to use transform.translate and rotation for movement.
I've tried changing the float to int and that came up with the same error.
Oh, I should also mention that EnemySpeed is taking a variable from the other enemy AI script.
Help?
Answer by VivekD · May 06, 2013 at 12:15 PM
Your problem here is you are trying to add two different data-types : Vector3 and float.
If you know the direction vector of the target gameobject you can get the future position this way :
var EnemySpeed = Target.GetComponent(FriendlyAI).InterceptSpeed;
var TargetFuturePosition : Vector3 = Target.position + TargetDirecion*EnemySpeed;
var FutureDirection : Vector3 = TargetFuturePosition - transform.position;
where TargetDirecion
is a Vector3 variable.
Hope this helps.
Thanks, I'd already worked out the position of the enemy object in the Intercept/Pursuit section before the if statements, so by adding that variable (which was Vector3) in place of TargetDirection as you suggested, it worked perfectly :D
I'd already figured I'd been trying to add two different data-types, I just couldn't figure out how to solve the problem.