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 Lionlev · Aug 26, 2012 at 01:51 PM ·

What's wrong with this code?

So basically I've got two objects box1 and box2 I've made box1 to move to the box2 (without accelerating or anything like that. just move and stop) but the problem is if I will turn box1 180, it will act weird, it will move around the map in circles until it reaches the box2 just like something goes in to the black hole... Here is the code:

 var target : Transform;
 var speed : float = 1;
 function Update() {
     transform.Translate((target.transform.position - transform.position).normalized * speed *Time.deltaTime);
 }

So basically I want box1 to move straight to the box2 without making curves and stuff Also would be awesome if it will face to the box2, no matter how I will turn it. HELP PLEAAASAEEE????????!?!?!?!

Comment
Add comment · Show 2
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 Piflik · Aug 26, 2012 at 02:06 PM 0
Share

I formated your code. Please use the 101010 button when you insert code into a post, so it is easier to read. Also 'help me' is not an useful tag.

avatar image Lionlev · Aug 26, 2012 at 02:08 PM 0
Share

I did, it didn't work as you saw... But thanks

2 Replies

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

Answer by Piflik · Aug 26, 2012 at 02:10 PM

Number1: target is already a transform, so target.transform.position is redundant. target.position is enough.

Number2: target.position - transform.position is a Vector3 in World Coordinates, whereas transform.Translate() uses local coordinates by default. This should work:

 transform.Translate((target.position - transform.position).normalized * speed *Time.deltaTime, Space.World);
Comment
Add comment · Show 4 · 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 Lionlev · Aug 26, 2012 at 02:18 PM 0
Share

Thanks! I've got another question: I've added the collision detection (using trigger) and I want the box1 when it touches the target (box2) to change the target to the "box3" var target : Transform; var speed : float = 1; function Update() { transform.Translate((target.position - transform.position).normalized * speed *Time.deltaTime, Space.World); } function OnTriggerEnter (hit:Collider) { if(hit.gameObject.name == "target"){

     Debug.Log("hit");
 }

}

But I am not sure how do I change the variable...

avatar image Lionlev · Aug 26, 2012 at 02:21 PM 0
Share

Weird 101010 button... Not working properly...

avatar image Piflik · Aug 26, 2012 at 02:24 PM 0
Share

I would use three variables: 1 to store box2, one to store box3 and the third as the target.

 var target1 : Transform;
 var target2 : Transform;
 var target : Transform;

 function Start() {
     target = target1;
 }

 function OnTriggerEnter() {
     target = target2;
 }

If you have more than 2 targets, I would use an array to store all of them and then just change the index variable.

avatar image Lionlev · Aug 26, 2012 at 02:29 PM 0
Share

Good idea! Thanks!

avatar image
0

Answer by Dasherz · Aug 26, 2012 at 02:03 PM

Do u have rigid bodies on them?

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 Lionlev · Aug 26, 2012 at 02:05 PM 0
Share

Yeah they do

avatar image Dasherz · Aug 26, 2012 at 02:16 PM 0
Share

To get them to look at each other look up the transform.LookAt() code it's pretty easy. try Turning off the rigidbodies. You only need it if u need them to collide or get effected by physics. You could also try turning off gravity and freezing constraints most likely the rotation.

Hope that helped

avatar image Lionlev · Aug 26, 2012 at 02:28 PM 0
Share

Yes it did! Thanks! (about the lookat) :D

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

9 People are following this question.

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

Related Questions

audio problems with my trigger script, help please 1 Answer

Does Unity Free download allow publishing online? 1 Answer

AppendProject error 2 Answers

Asset store 1 Answer

I want this script to destroy the enemy 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