• 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
Question by WesterlyCarrot9 · Mar 10, 2013 at 03:11 AM · c#movementplayerenemyclick

Implement moveSpeed to this object script?

How can i implement moveSpeed into this script here? Right now when i click on the enemy, the player justs teleports on him and then falls and i don't want that. I want him to move based on a speed you know move like normal :P

 using UnityEngine;
 using System.Collections;
 
 public class MoveObject : MonoBehaviour {
     
     public Transform target;
     
     void Start() {
         target = GameObject.FindWithTag("Enemy").transform;
     }    
     
     void Update() {
         if(Input.GetMouseButtonUp(0)) {
             transform.position = Vector3.Lerp(transform.position, target.position, Time.time);
         }
     }
 }    
Comment
Statement

People who like this

1 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Statement · Mar 10, 2013 at 04:37 AM

You should increment the position over several frames. One way to do it is to use Vector3.MoveTowards.

 using UnityEngine;
 using System.Collections;
  
 public class MoveObject : MonoBehaviour {     
     public Transform target;
     public float moveSpeed; 
     bool isMoving;

     void Start() {
         target = GameObject.FindWithTag("Enemy").transform;
     }  
  
     void Update() {
        if (Input.GetMouseButtonUp(0))
            isMoving = true;

        if (isMoving && target)
            transform.position = Vector3.MoveTowards(
                                 transform.position, 
                                 target.position, 
                                 moveSpeed * Time.deltaTime);
     }
 }  


Comment
WesterlyCarrot9

People who like this

1 Show 3 · 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 WesterlyCarrot9 · Mar 10, 2013 at 04:59 AM 0
Share

Thanks a lot for that :D The funny thing now is that as soon as i hit play it moves on its own before i can click on the enemy :S Also is there a way to make it be active at the same time with my ClickToMove script? I use this one... http://wiki.unity3d.com/index.php/Click_To_Move_C

When both are active the player stucks in one place and the whole camera starts shaking :S

avatar image Statement · Mar 10, 2013 at 05:06 AM 0
Share

Hi, I assumed that since you set target in Start that you hadn't set it in the inspector also... The revised code includes a isMoving variable that needs to be set before movement occur.

I have no idea if it will work with that other script. I guess is no, since both of them appear to want to control the object if I understood it correctly. Use one or the other, not both. It's like having two drivers in a car, both fighting for the steering wheel.

avatar image WesterlyCarrot9 · Mar 10, 2013 at 05:08 AM 0
Share

I see. Thank you.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Unity how to make different enemy spawn one at a time 1 Answer

EnemyHover Script Error? 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

Problem with player movements 1 Answer

Inheritance with enemies 1 Answer


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