x


click to move with constant speed

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 :)

more ▼

asked Jun 14 '11 at 01:02 AM

ads49 gravatar image

ads49
1 2 2 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Click to move is quite simple:

// Click To Move script

// Moves the object towards the mouse position on left mouse click

var smooth:int; // Determines how quickly object moves towards position

private var targetPosition:Vector3;

function Update () {
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        var playerPlane = new Plane(Vector3.up, transform.position);
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hitdist = 0.0;

        if (playerPlane.Raycast (ray, hitdist)) {
            var targetPoint = ray.GetPoint(hitdist);
            targetPosition = ray.GetPoint(hitdist);
            var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
            transform.rotation = targetRotation;
        }
    }

    transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}

You could find a lot of useful scripts here:

http://www.unifycommunity.com/wiki/index.php?title=Scripts

more ▼

answered Jun 14 '11 at 01:25 AM

Dreamer gravatar image

Dreamer
1.5k 39 49 66

Thanks for the quick reply, I have tried that script, all it does for me is rotate the avatar toward where I pointed, and not actually "move" the avatar across the screen. Forgot to mention I don't want the character to rotate anyway (suppose that could be fixed with a joint). Thanks tho.

Jun 14 '11 at 01:33 AM ads49

It should able to move as long as you click on a collider. If not, modify last line to be transform.position = Vector3.Lerp (transform.position, targetPosition, 100*Time.deltaTime * smooth);

Jun 14 '11 at 01:45 AM Dreamer

Still the same result. Could you possibly help with the code I pasted? Thanks.

Jun 14 '11 at 01:51 AM ads49

I don't know about StartCoroutine and IEnumerator, so I not sure what is wrong with it. The script is workable, I have already tested before.

Jun 14 '11 at 02:01 AM Dreamer

I had to use the script CoUpdate because yield doesn't work with Update. Here's the link: http://www.unifycommunity.com/wiki/index.php?title=CoUpdate

Jun 14 '11 at 02:08 AM ads49
(comments are locked)
10|3000 characters needed characters left

It's not moving because your not assigning a value to "smooth".

Set it to 1 or something in the inspector (or hard code it) then it will move.

issue is the speed is not constant wit this script, the further away from the object you click, the faster it moves.

more ▼

answered Apr 25 '12 at 08:17 PM

Skonk gravatar image

Skonk
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x1047
x525
x19

asked: Jun 14 '11 at 01:02 AM

Seen: 1791 times

Last Updated: Apr 25 '12 at 08:17 PM