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
1
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
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 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
Add comment · 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 ClickTo$$anonymous$$ove script? I use this one... http://wiki.unity3d.com/index.php/Click_To_$$anonymous$$ove_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 is$$anonymous$$oving 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.

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

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] Two Mobile Single Stick Controllers and Click Controls not working synonymous with each other 0 Answers

c# array of game objects movement help 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