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 Danielman2 · Nov 23, 2013 at 09:10 PM · move an objectfunction updatetransform.

Stop a enemy by pointing a light at it.

I want to be enabled to stop a monster like speed to = 0 by holding my RMB Pointing at him.

I´m sorry i found my own way of doing this :D

But you helped me somehow, I took a few lines from each code, So Thanks!

If you are wondering what game i´m making, http://holan.ucoz.com check this out!

Comment
Add comment · Show 3
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 Gruffy · Nov 23, 2013 at 11:48 PM 0
Share

Okay. I will come back later to answer this but I would like to know if you have a raycast from your player (particularly casting in the direction of your flashlight/spotlight), if so, then use the Unity referenced Physics.RayCast to cast out an effective line that can be broken by another object intersecting it, this would set you up to find an object with the ray and then do something (like, telling the object to stop moving when the ray is pointing at them ). Im around coding for a bit, so get back and let me know how you have already approached this. $$anonymous$$ainly, you should at least read this first, perhaps even copy the script below (found on reference website) and try it out in a Unity scene.

Link to reference website: link text Unity Reference Code:

         function Update () {
             var fwd = transform.TransformDirection (Vector3.forward);
             if (Physics.Raycast (transform.position, fwd, 10)) {
                 print ("There is something in front of the object!");
 //do something here... like create a hit variable and use 
 //"hit.gameobject.tag" to access it and control the component
 // moving the object towards the player
             }
         }


avatar image Gruffy · Nov 24, 2013 at 03:54 PM 0
Share

Okay, well done for sorting out your own problem, that`s the way to do it dude ! As I have given you basically an entire script that will do exactly as you ask succinctly, could you please mark it as your answer as it may help others who come across this issue in the future who may either be using jS or C# (to accomplish what youve asked and had answered by myself etc) Cheers bud. Gruffy

PS. I also take it you will not be need ing a raycast equivalent to show you that method of approach against this one :) Let me know :)

avatar image birdonblack · May 31, 2015 at 12:30 PM 0
Share

any more information on this?

4 Replies

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

Answer by Gruffy · Nov 24, 2013 at 02:02 PM

Hey Danielman2 , here are two scripts that do the same thing, one in JS and one is C# as im not sure what you are working in but anyway....

INSTRUCTION: 1.Make a new cube object in scene 2.Attach this script to it in the inspector. 1. Drag your object that the enemy should target (i assume your player gameObject and drop it into script in inspector). 2. Be aware that I have placed a public slot for the actual transform you are moving, this is not necessary as you are already addressing it as script would be attached to it, but it atleast helps you understand the specific objects you are manipulating here. (change the public part to read "private" or just add the object to script in inspector, no harm will come from it) 3. Press play.

WHAT SHOULD HAPPEN NOW?...... you should see the transform moving towards your player (if placed in sight of player view, it will then move towards you slowly...using your mouse, place the cursor over the object moving towards you and press and hold "left click" it should stop....release the mouse button and it should start moving)

I will return with a RayCast option too, but i liked that dumytruncs answer, because effectively he is right.

btw, move to C#, Coding canbe hard generally but C#`s not so hard to get your head around and in a few months time you can return to here and say thanks Gruffy, you were right about moving languages and that you love C# and cant think of touching Unity with that horrible loose language that has its place, but maybe not in Unity for much longer (i dont know that, im just saying) :) Hope it all helps and if you can mark my answer if it does the job dude, that would be great Gruffy

c# version

 public class TranslateObjectToTargetAndMouseControl : MonoBehaviour
 {
 
     public Transform myTrans;
     public Transform target;
     public float speed = 1.0f;
     // Use this for initialization
     void Start ()
     {
         //cache you public gameobject/transform
         myTrans = this.transform;
     
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         //interpolate from current position to the target over time
         myTrans.position = Vector3.Lerp (myTrans.position, target.position, speed * Time.deltaTime);
     }
 
     void OnMouseDown()
     {
         speed = 0.0f;
     }
 }



javascript version

pragma strict

 public class TranslateObjectToTargetAndMouseControl extends MonoBehaviour
 {
     var myTrans : Transform;
     var target : Transform; //put the object you want the transfrom to move towards (this would be you player most likely)
     var speed : float = 1.0f; //the speed at which your object is moving towards player
 
     function Start()
     {
         //cache you public gameobject/transform
         myTrans = this.transform;
 
     }
 
     function Update () 
     {
         //interpolate from current position to the target over time
         myTrans.position = Vector3.Lerp (myTrans.position, target.position, speed * Time.deltaTime);
     }
 
     function OnMouseDown()
     {
         //when left mouse button clicked it will stop speed
         speed = 0.0f;
     }
     function OnMouseUp()
     {
         //when left mouse click is lifted, speed will return to 1.0 and start moving
         speed = 1.0f;
     }
 }



PS..I JUST TESTED THIS AND IT WORKS PERFECTLY, just in case you feel it doesnt

Comment
Add comment · Show 1 · 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 haim96 · Nov 24, 2013 at 07:56 PM 0
Share

and... getting answers in form of "ready to implemented scripts" is always nice but i really do suggest that you watch ALL the video tutorials in the learn section. i learned everything i need from there. hell! i learned to code C# from scratch from these videos... :)

avatar image
0

Answer by dumytrunc · Nov 24, 2013 at 12:34 AM

 function OnMouseDown ()
 {
     speed=0;
 }

Add this to the script. OnMouseDown is called when you hold click on the enemy. OnMouseUp is called when you release the click, and you can use it to change the speed back to 5, or whatever.

Hope it helps!

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 Danielman2 · Nov 24, 2013 at 01:06 PM

Gruffy, It would not work??

Comment
Add comment · Show 1 · 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 Gruffy · Nov 24, 2013 at 01:31 PM 0
Share

hey bud, the code above merely demonstrated that you can get feedback from your raycast its a snippet from the reference sit.. I will return in half hour with a result for you but dunytrunc has also pointed out a fairly good alternative. If you apply the On$$anonymous$$ouseDown function by adding it to a script, and attaching it to your enemy object.....when you mouse click on the object co$$anonymous$$g towards you it will indeed stop the object as long as you have a variable that deter$$anonymous$$es its speed or motion The l0gic is basically using the mouse input to return a boolean based on the mouse hitting your objects collider when you mouse over that object, with the On$$anonymous$$ouseDown() code attached to it you are saying "when my object is in range of the mouse and has a mouse action attached to it via script, carry out the operation you specified in code when i click the left $$anonymous$$ouse button Down function". arrgh, im waffling, brb with your answer dude..gimme 20 $$anonymous$$s matey Gruffy

avatar image
0

Answer by haim96 · Nov 24, 2013 at 01:23 PM

watch this video. it's about raycast laser and fun stuff. the code is c# but should be easy to rewrite in java.

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/fun-with-lasers

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

20 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

Related Questions

Endless runner, road segments spawn speed 0 Answers

How to move a Transform on top of a series of Transforms 0 Answers

How to remove decimals from Vector3.Distance 3 Answers

My character isn't moving 0 Answers

Rubber banding camera (help!) 2 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