- Home /
Two problems One code
When I run this code I get two problems, the most serious one is an error: Assets/Scripts/AI.js(26,46): BCE0017: The best overload for the method 'UnityEngine.Quaternion.Slerp(UnityEngine.Quaternion, UnityEngine.Quaternion, float)' is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion, float)'.
The next, if I comment out the 26th line and when I move the player closer to the capsule object I attached the script too it gets another error: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) AI.Attack () (at Assets/Scripts/AI.js:30) AI.Update () (at Assets/Scripts/AI.js:20)
The code:
var Distance;
var Target : Transform;
var LookAtDistance = 25.0;
var attackRange = 15.0;
var movespeed = 6.0;
var Damping = 4.0;
function Update() {
Distance = Vector3.Distance(Target.position, transform.position);
if(Distance < LookAtDistance) {
renderer.material.color = Color.yellow;
LookAt();
}
if(Distance > LookAtDistance) {
renderer.material.color = Color.green;
}
if(Distance < attackRange) {
renderer.material.color = Color.red;
Attack();
}
}
function LookAt() {
var rotation = Quaternion.LookRotation(Target.position - transform.position);
//transform.rotation = Quaternion.Slerp(transform.position, rotation, Time.deltaTime * Damping);
}
function Attack() {
transform.Translate(Vector3.foward * movespeed * Time.deltaTime);
}
Could anyone please help me with these problems? Thanks in advance.
Quaternion is a rotation. You need a start rotation, end rotation and a scaling factor (0 to 1) for Q.slerp
Your error says you are inputting a Vector3 ins$$anonymous$$d.
(UnityEngine.Vector3, UnityEngine.Quaternion, float)
A NullReferenceEception occurs when something can't be found, like an object.
You have
var Target : Transform;
but it's an empty container specifying variable type but not filling it wil viable information. "I see its a Transform but the Transform of What?"
Did you copy and paste the code? If it is copied, then in line 30 you should have Vector3.forward
, not Vector3.foward
Answer by propro2000 · Oct 26, 2013 at 07:02 AM
Thanks for the help guys. Here is finished (Mostly) script for anyone who has trouble.\
var Distance;
var Target : Transform;
var Player : GameObject;
var LookAtDistance = 50.0;
var ChaseRange = 15.0;
var AttackRange = 3.0;
var movespeed = 6.0;
var Damping = 4.0;
var damamount = 124;
var attackrepeattime = 2;
private var attacktime : float;
var Controller : CharacterController;
var Gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
private var maxSpeed = movespeed;
var idleanimation : AnimationClip;
var attackanimation : AnimationClip;
var walkanimation : AnimationClip;
var dieanimation : AnimationClip;
function Start() {
attacktime = Time.time;
}
function Update() {
Distance = Vector3.Distance(Target.position, transform.position);
if(Distance < LookAtDistance) {
LookAt();
}
if(Distance > LookAtDistance) {
animation.Play(idleanimation.name);
}
if (Distance < AttackRange) {
Attack();
}
else if(Distance < ChaseRange) {
Chase();
}
}
function LookAt() {
if(animation.isPlaying == false) {
animation.Play(idleanimation.name);
}
var relativePos = Target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
}
function Chase() {
if( Distance > AttackRange) {
animation.Play(walkanimation.name);
movespeed = maxSpeed;
}
MoveDirection = transform.forward;
MoveDirection *= movespeed;
MoveDirection.y -= Gravity * Time.deltaTime;
Controller.Move(MoveDirection * Time.deltaTime);
}
function Attack() {
if(Time.time > attacktime) {
animation.Play(attackanimation.name);
Player.BroadcastMessage("TakeDamage", damamount);
attacktime = Time.time + attackrepeattime;
}
}
function ApplyDamage() {
LookAtDistance += LookAtDistance / 5;
ChaseRange += ChaseRange / 5;
movespeed += movespeed / 5;
damamount += damamount / 10;
}