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 Smithy311 · Dec 09, 2014 at 07:37 AM · javascriptgameobjectrigidbodyraycast

Creating a teleportation gun

Hi, I've been trying to create a teleportation gun using raycasts and rigidbody's.

I have made a basic raycast gun

     var bullet : Rigidbody;
     var bulletPoint : Transform;
     
     function Update () 
     {
         var hit : RaycastHit;    
         
         if (Input.GetButtonDown("Fire1"))
         {
             var fwd = transform.TransformDirection (Vector3.forward);
     
             if (Physics.Raycast (transform.position, fwd))
                 print ("Raycast Hit!");
             else
                 print ("Raycast Didn't Hit!");
     
             var bulletInstance : Rigidbody;
     
             bulletInstance = Instantiate(bullet, bulletPoint.position, bulletPoint.rotation); //Object to clone, origin of bullet, rotation
             bulletInstance.AddForce(fwd * 500); //Moving projectile
             
             Quaternion.LookRotation(hit.normal));
         }
     }

I also have a basic teleport script:

 var target : Transform;
 
 function Update () {
  
 }
  
 function OnTriggerEnter (col : Collider) {
  
         if(col.gameObject.tag == "teleport") {
                 this.transform.position = target.position;
         }
 }

I'm trying to combine these two, so when the projectile hits a wall, it teleports the player to where it hit.

Any help is greatly appreciated.

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 taxvi · Dec 09, 2014 at 08:13 AM 0
Share

well, thing is, this all looks fine except that when you add such a big force on 20th line the object will shoot so fast that unity may not be fast enough to detect the collision with the wall. I mean, in the first frame the object is in front of the wall, in the second it's already past the wall making unity think the collision never happened.

avatar image taxvi · Dec 09, 2014 at 08:17 AM 0
Share

also, if your second script is attached to your hero, most probably nothing will happen because the OnTriggerEnter will check if the HERO is entering any trigger, but what you really need is to perform the same check in the bullet object. if the bullet hits the wall, take your hero and put it for the position of the bullet, destroy bullet.

1 Reply

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

Answer by Smithy311 · Dec 09, 2014 at 06:52 PM

I got it working!

Gun Script:

 #pragma strict
 
 var bullet : Rigidbody;
 var bulletPoint : Transform;
 var teleport;
 
 function Update () 
 {
     var hit : RaycastHit;    
           
     if (Input.GetButtonDown("Fire1"))
     {
         var fwd = transform.TransformDirection (Vector3.forward);
 
         if (Physics.Raycast (transform.position, fwd))//Check if raycast hit
             print ("Raycast Hit!");
         else
             print ("Raycast Didn't Hit!");
 
         var bulletInstance : Rigidbody;
 
         bulletInstance = Instantiate(bullet, bulletPoint.position, bulletPoint.rotation); //Object to clone, origin of bullet, rotation
         bulletInstance.AddForce(fwd * 500); //Moving projectile
         //Destroy (bulletInstance.gameObject, 1);
         
         //Quaternion.LookRotation(hit.normal);
     }
 }

Teleport Script:

 var target : Transform;
 var player : GameObject;
 
 function OnTriggerEnter( col : Collider )
 {
     player = GameObject.Find("Player");
     
     if(col.gameObject.tag == "teleport")
         player.transform.position = target.position;
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to stop a rigidbody from bouncing off when colliding 2 Answers

Dragging object out of position 2 Answers

OnGUI nullreference 0 Answers

Detect Object that are hit by a ray 3 Answers

Need help with limiting rotation. 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