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 greatwhiteshark17283 · Jun 08, 2013 at 03:20 PM · movementtransformlooptriangles

Movement Script Help

I'm not that good at programming, but I attempted to make a script that makes a game object go to one transform and then another and it will travel in a triangle over and over again. When I made the script, the game object went towards the first transform and just circled around it. How can I make it so then the game object travels in a triangle? Here is my script:

 var moveSpeed = 5;
 var targetOne : Transform;
 var targetTwo : Transform;
 var targetThree : Transform;
 var damping = 2;
 
 function Update(){
     if(gameObject.transform.position == targetOne.position){
         Invoke("lookAt", 0.0);
         Invoke("attack", 0.0);
     }
     if(gameObject.transform.position == targetTwo.position){
         Invoke("lookAtTwo", 0.0);
         Invoke("attack", 0.0);
     }
     if(gameObject.transform.position == targetThree.position){
         Invoke("lookAtThree", 0.0);
         Invoke("attack", 0.0);
     }
             
 }
 function lookAt ()
 {
 var rotation = Quaternion.LookRotation(targetTwo.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }
 
 function attack ()
 {
  
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
 }
 
 function lookAtTwo ()
 {
 var rotationTwo = Quaternion.LookRotation(targetThree.position - transform.position);
 transform.rotationTwo = Quaternion.Slerp(transform.rotationTwo, rotationTwo, Time.deltaTime * damping);
 }
 
 function lookAtThree ()
 {
 var rotationThree = Quaternion.LookRotation(targetOne.position - transform.position);
 transform.rotationThree = Quaternion.Slerp(transform.rotationThree, rotationThree, Time.deltaTime * damping);
 }
 
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
0
Best Answer

Answer by jerobinson · Jun 08, 2013 at 04:22 PM

The problem I believe you are having is that you are checking for when your position is EXACTLY equal to the targets position. This provides such a little amount of room for error, that your dampened rotation makes you circle around the target, but not actually get close enough to it to trigger the next target. The best way to avoid this happening would be to check when you are within a certain amount of distance to your current target.

For Example:

 if((transform.position - target.position).magnitude < 1f) {
     Invoke("LookAt" 0.0);
     Invoke("Attack" 0.0);
 }

The way that I would go about this is a little bit different from yours. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleMovement : MonoBehaviour {
     //Here I am defining a new array called "targets", with an array length of 3.
     //This means that there will be 3 transforms in my array.
     //I can assign these in the inspector window.
     public Transform[] targets = new Transform[3];
     //Here I am defining an int variable which i can use to change the current "targets" transform to use.
     public int curTarget = 0;
     
     void Update() {
         //Check if we are within 1 unit of our target (reduces risk of error - object could miss target which is what causes yours to circle the target)
         if((transform.position - targets[curTarget].transform.position).magnitude < 1f) {
             //When we are within one unit of target, switch to the nex target(or the next transform in the array).
             curTarget++;
             //When our current target reaches the length of the array, we will loop it back to 0 so that it repeats.
             if(curTarget > 2) {
                 curTarget = 0;
             }
         }
         //Move forwards
         transform.Translate(Vector3.forward * 3.0f * Time.deltaTime);
         //Always look at the current target
         transform.LookAt(targets[curTarget].position);
     }
 }

Sorry that is is C#, but i have commented it heavily to make it easy to understand.

Hope this helps ;)

Comment
Add comment · Show 2 · 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 greatwhiteshark17283 · Jun 08, 2013 at 07:12 PM 0
Share

Thanks a lot! The script works fine!

avatar image jerobinson · Jun 09, 2013 at 02:50 AM 0
Share

Not a problem. Glad to hear it helped!

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

15 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

Related Questions

Updating rotation of client not working - strange error 0 Answers

Network Trasnform Interpolation Factor 2 Answers

Can you pass along something like transform.position.x or y? not the value but the VARIABLE? 2 Answers

how to move a character in a straight line path? 0 Answers

Why does the player keep pointing in a specific direction after moving? 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