• 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 mustang4484 · Jun 22, 2018 at 10:41 AM · scripting problem

Point to click without navmesh agent and speed problem

Hi guys

I would like to make a simple script where on click mouse button my object go to destination. I've this script: if (Input.GetMouseButtonDown(0)) { RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)&&hit.transform.gameObject.tag=="terreno") { player.transform.position = hit.point;
}

When click on mouse, my object go to destination immediately but I want to insert a "float speed". I had try to use a move toward but doesn't work. Can you help me?

THK

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
0
Best Answer

Answer by LeeroyLin · Jun 22, 2018 at 01:58 PM

There are many ways to do it, and I write an example for you.

 /// <summary>
 /// The Player
 /// </summary>
 public GameObject player;
 
 /// <summary>
 /// How long the player move for every frame.
 /// </summary>
 public float moveDisPerSec = 1;
 
 /// <summary>
 /// Record the mouse position.
 /// </summary>
 Vector3 destination;
 
 private void Start()
 {
     // At first, set the destination to player's position.
     destination = player.transform.position;
 }
 
 private void Update()
 {
     // Check the mouse left button click
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit hit;
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100) && hit.transform.gameObject.tag == "terreno")
         {
             // When click on the terreno, record the position.
             destination = hit.point;
         }
     }
 }
 
 private void FixedUpdate()
 {
     // get the distance between the player and the destination pos
     float dis = Vector3.Distance(player.transform.position, destination);
     if (dis > 0)
     {
         // decide the moveDis for this frame. 
         //(Mathf.Clamp limits the first value, to make sure if the distance between the player and the destination pos is short than you set,
         // it only need to move to the destination. So at that moment, the moveDis should set to the "dis".)
         float moveDis = Mathf.Clamp(moveDisPerSec * Time.fixedDeltaTime, 0, dis);
 
         //get the unit vector which means the move direction, and multiply by the move distance.
         Vector3 move = (destination - transform.position).normalized * moveDis;
         player.transform.Translate(move);
     }
 }

And if there is a RigidBody component in your player object and you want a physical effect, maybe you can use AddForce() function instead of Translate(). Hope this can help you.

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.

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

150 People are following this question.

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

Related Questions

How to deserialize properties with JsonUtility 0 Answers

Learn: Survival Shooter aim offset from mouse position 1 Answer

fps goes down when moving the camera 1 Answer

Inverted Char control 1 Answer

Get Object after Frustum Culling or Get Object in Camera 0 Answers

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