Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 edwaudo · Oct 01, 2011 at 12:31 AM · raycasting

Trying to make a tractor beam

I'm trying to make a tractor beam using RayCast. The Ray works but I can't figure out how to make the objects come to me. How can I make the objects I tag move towards the player using the ray?

var MyColor : Color = Color.red; var target : Transform; var come : float = 5;

function Awake() { target = gameObject.Find("Player").transform; }

function Start() { Screen.showCursor = false; }

function Update () { if(Input.GetButton("Fire1")) { // Get the ray going through the center of the screen var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0)); // Do a raycast var hit : RaycastHit; if (Physics.Raycast (ray, hit,100)) { if(hit.collider.gameObject.tag == "Pullable") { transform.position = Vector3.MoveTowards(transform.position, target.position,Time.deltaTime * 5); transform.LookAt(target); print ("I'm looking at " + hit.transform.name); } } else { print ("I'm looking at nothing!"); } Debug.DrawLine (ray.origin, hit.point, Color.red); } }

Comment
Add comment · Show 1
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 SilverTabby · Oct 01, 2011 at 12:34 AM 0
Share

The code you have here makes the object this Script is attached to move towards the the object hit by the ray. It should be as simple as swapping the transforms that you are affecting.

1 Reply

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

Answer by aldonaletto · Oct 01, 2011 at 01:04 AM

As @Silver Tabby said, you're using the wrong transforms:

 var MyColor : Color = Color.red;
 var target : Transform;
 var come : float = 5;
 
 function Awake()
 {
     target = gameObject.Find("Player").transform;
 }
 
 function Start()
 {
     Screen.showCursor = false; 
 }
 
 function Update () 
 {
     if(Input.GetButton("Fire1"))
     {
 // Get the ray going through the center of the screen
         var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
 // Do a raycast
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit,100))
         {       // if the object hit is Pullable...
             if(hit.transform.tag == "Pullable")
             {       // move the object hit!
                 hit.transform.position = Vector3.MoveTowards(hit.transform.position, target.position, Time.deltaTime * 5);
                 // makes the object hit look to the target    
                 hit.transform.LookAt(target);
                 print ("I'm looking at "+hit.transform.name);
             }
         }
         else
         {
             print ("I'm looking at nothing!");
         }
         // you will have no hit.point if nothing was hit - use DrawRay instead    
         Debug.DrawRay (ray.origin, ray.direction * 100, Color.red);
         // but since the ray starts at the center of the viewport, you will    
         // only see a red dot anyway.
     }
 }
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 edwaudo · Oct 01, 2011 at 01:29 AM 0
Share

thanks guys that solved it.

avatar image Snownebula · Dec 12, 2013 at 08:50 AM 0
Share

NullReferenceException: Object reference not set to an instance of an object TractorBeam.Awake () (at Assets/SCRIPTS/TractorBeam.js:7)

avatar image AlucardJay · Dec 12, 2013 at 08:57 AM 0
Share

Then you don't have an object in your scene named Player

search NullReferenceException

If there is no gameObject found named Player, the transform cannot be accessed. The gameObject returns a null.

avatar image Headworker · Jan 27, 2014 at 12:39 PM 0
Share

I know this is solved already, but from my experimentations I can recommend the following:

Afaik the best simulation method for a tractorbeam would be to use a collider marked as trigger with a script that applies force to all rigidbody in the direction of the beam emmitter to all rigidbodies within the beam area via OnTriggerEnter

You have several advantages with that:

  • You can easily modify the "influence shape" of the tractorpull.

  • You can pull several objects at once.

  • In my imagination a tractor beam resembles more a "forcefield" which is able to pull objects which are obstructed by others as long as they are in the force area.

  • If you use the physics engine, the pull is more natural with inertia etc.

If you want to try out this route and still questions left, just ask!

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

7 People are following this question.

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

Related Questions

Unity UI click through problem ( seems to be a common problem :( ) 0 Answers

2D moblie game raycast problem 1 Answer

How to check if 2 collisions are on the same objects ? (is player on the same ground than enemy?) 2 Answers

Managing & raycasting to multiple world space canvases 0 Answers

Raycast to an object behind another object without ignoring raycast 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