Hi all. Been reading here a little while, very good site. I am new to unity and javascript so bare with me please. I’m working on a old point and click style adventure game and I needed a script for click to move w/ constant speed for the avatar, but I haven’t been able to find one so I figured I’d give it a shot. It compiles, but gives me a few errors when I click. Here’s the errors:
NullReferenceException
SmoothMover+$SmoothMover$24+.MoveNext () (at Assets/Scripts/SmoothMover.js:33)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
:MoveNext() (at Assets/Scripts/SmoothMover.js:26)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
$:MoveNext() (at Assets/Scripts/SmoothMover.js:14)
and the code…
private var player : Transform;
private var rayhitPoint : Vector3;
var moveSpeed : float = 1;
function Start()
{
StartCoroutine("CoStart");
}
function CoStart() : IEnumerator
{
while (true)
yield CoUpdate();
}
function CoUpdate() : IEnumerator
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit))
{
rayhitPoint = hit.point;
yield SmoothMover();
}
}
}
function SmoothMover() : IEnumerator
{
var dist = Vector3.Distance(player.transform.position, rayhitPoint);
for (i = 0.0; i < 1.0; i += (moveSpeed * Time.deltaTime) / dist)
{
transform.position = `Vector3.Lerp(player.transform.position, rayhitPoint, i);
yield;
}
}
Am I not sure what’s going on here, but any insight would be greatly appreciated. Thank you.
-Ads
*edit: sorry for the formating…first post and all