Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ads49 · Jun 14, 2011 at 01:02 AM · charactercontrollerclicktomove

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dreamer · Jun 14, 2011 at 01:25 AM

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ads49 · Jun 14, 2011 at 01:33 AM 0
Share

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.

avatar image Dreamer · Jun 14, 2011 at 01:45 AM 0
Share

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

avatar image ads49 · Jun 14, 2011 at 01:51 AM 0
Share

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

avatar image Dreamer · Jun 14, 2011 at 02:01 AM 0
Share

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.

avatar image ads49 · Jun 14, 2011 at 02:08 AM 0
Share

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

avatar image
0

Answer by Skonk · Apr 25, 2012 at 08:17 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Scripting help 1 Answer

Player Animation and control panel 2 Answers

My Character doesnt move since ive put aim 0 Answers

No slipping off edges with Character Controller 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges