• 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
1
Question by berting01 · Jan 20, 2012 at 03:40 PM · movementraycastmouse

problem with mouseclicke movement

I have a code which I also got from the internet. Here is the code:

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

}

the problem with this code is that when I click to one place. my character is only facing to the place where I clicked and does not move there. do you know whats missing in this program? and also how will I be able to put the animation walk that after the character moved to the position it wil return to idle animation.. thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jan 20, 2012 at 05:03 PM

The only weird thing in your code is smooth being an int - it should be a float. But as far as I know, int float* results in a float, thus this should not kill the movement. Declare smooth as float and assign a reasonable value (5 takes about 1 second).
Another suggestion: the movement above will start fast and slow down near to the destination. If you want a constant speed, use Vector3.MoveTowards:

   transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);

Where speed is in meters per second. As occurs with Lerp, the object will never pass the targetPosition.

Comment
Add comment · Show 4 · 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 berting01 · Jan 21, 2012 at 06:58 AM 0
Share

ok thanks.. but what about the smooth thing? how should i put a value? is it something like this ? var smooth:float = 2; ? thanks

avatar image Lo0NuhtiK · Jan 21, 2012 at 07:25 AM 0
Share

yes @berting01
var smooth : float = 2 ;
or var smooth : float = 23.0434 ;
... or whatever so long as its a numeric value ;
Don't forget to mark aldo's answer as the accepted if it helped you out.

avatar image berting01 · Jan 21, 2012 at 07:47 AM 0
Share

one more thing... Where should i put my animation named walk and play here. I am able to put the animation.play("walk") but the problem is the moment i clicked. it wont stop. @lo0Nuhti$$anonymous$$

avatar image berting01 · Jan 21, 2012 at 08:29 AM 0
Share

the animation walk will play when moving to the clicked direction and the animation idle will play after reaching the position. how do i do that here

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

click to move workig script!! but pls help with rotation!! :( 1 Answer

Camera/mouse movement issues "almost but no dice" 1 Answer

Using Raycast to detect what's hit and if it's tagged 2 Answers

Object Movement via Mouse Click? 3 Answers

Multiple colliders using IPointerEnter/ExitHandler 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges