Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Althaen · Jan 14, 2013 at 04:16 AM · c#inputmovment

Click to move issue.

I'm trying to make a script that moves the character where the player clicks. I have it mostly working apart from making the character continue moving after the mouse button is released. I also need the player to stop moving upon reaching it's destination.

Would appreciate if someone could point out what I'm doing wrong or missing.

 using UnityEngine;
 using System.Collections;
 
 public class InputManager : MonoBehaviour {
     Ray ray = new Ray();
     RaycastHit hit = new RaycastHit();
     public float playerSpeed = 5;
     private bool isMoving;
     public GameObject movingPivot; //empty object placed exactly beneath player for position
     
     void Start(){
         movingPivot = GameObject.Find("MovingPivot");
     }
     
     void Move(){
         transform.Translate((hit.point - movingPivot.transform.position).normalized * playerSpeed * Time.deltaTime);
     }
     
     void Update (){
         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if(Input.GetButtonDown("Fire1")){
             if(Physics.Raycast (ray, out hit, 1000)){
                 if(hit.collider.gameObject.tag == "Terrain"){
                     isMoving = true;
                     Move ();
                 }
             }
         }
     }
 }
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 Timer · Jan 14, 2013 at 11:14 AM

I think, I got the problem, the isMoving boolian you have created you are turning that on but not using it.

Inside the move function do something so that like

if(isMoving){

translate

}

and don't forget the turn the isMoving off when you have reached your target,

If I had to do this I would simply make a prefab that has trigger component attached to it and instantiating it at the hit point of the ray so every time user clicks the mouse it creates the small trigger in the terrain and small script would be like this:

void OnTriggerEnter(Collider other){

if(other.comparetag("Player")) {

other.getcomponent().isMoving = false;

destroy(this.gameObject);

}

}

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
avatar image
0

Answer by CodeMasterMike · Jan 14, 2013 at 08:22 AM

I would use Slerp/Lerp instead to move between two different positions. Something like this (pseudo-code!):

     Vector3 goalPosition = Vector3.zero;
     Vector3 startPosition = Vector3.zero;
     float timeMovement = 10.0f;
     float currentTimeProcentage = 0.0f;
     float startTime = 0.0f;
     float endTime   = 0.0f;
 
 Update
 {
     ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
       if(Input.GetButtonDown("Fire1"))
       {
          if(Physics.Raycast (ray, out hit, 1000))
          {
             if(hit.collider.gameObject.tag == "Terrain")
             {
                 goalPosition = hit.point;
                 startPosition   = this.transform.position;
                 isMoving = true;
                 
                 startTime = Time.time;
                 endTime = (startTime + timeMovement);
                 
             }
          }
        }

   if(isMoving == true)
   {
     Move ();
   }
 }
 
 void Move()
 {
     currentTimeProcentage = (endTime / Time.time);
     transform.Translate.position = Vector3.Slerp(startPosition, goalPosition, currentTimeProcentage);
     if(currentTimeProcentage >= 1.0f)
     {
         isMoving = false;
     }
 }

As you can see in the code, I update the start and goal position every time the button is pressed so that the Slerp/Lerp function knows where to start and where to go to. I also reset the procentage counter for the Slerp/Lerp function.

This is a very simple example I just wrote out of my head. It is not tested so don't just copy it. But it hopefully shows a way for you to easially move a character between two points when the mouse button is pressed.

Good luck!

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Input System binding with one modifier composite value problem 1 Answer

C# Touch Script - Fix GameObject touch. 0 Answers

Using Oculus headgear position as input. 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