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 /
This question was closed May 18, 2015 at 09:59 PM by Baste for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Fewpwew130 · Feb 04, 2015 at 01:59 PM · rayboundsteleportblink

[ // commented] [C# script attached] Teleport (blink) script

Hello guys, I am making an FPS. I would like to make a character blink (short distance teleport). I would like my character to teleport in the direction he is going.

I've written and commented the script for the most part. I ask for your help to comment the logic in the script and operators I used (whether I did the rigth thing or not). I am particularly interested in how to get the object hit by a ray and check it's width.

Here's the script: (with //commented part). Thanks a lot!!!

 using UnityEngine;
 using System.Collections;
 
 public class teleport : MonoBehaviour {
 
     public Vector3 old_position;
     public Vector3 newVector;
 
     RaycastHit hit;
 
 
     void Start()
     {
         old_position = transform.position;
     }
         
     void Update() {
 
         //if a player is moving
         if(transform.position != old_position)
         {
             newVector = transform.position - old_position;
             Vector3 coordinates = transform.position;
             
             //if a player decides to shoot while moving
             if (Input.GetMouseButtonDown(0))
             {
                 //let's check if the space we want to teleport is free
                 //let's shoot the ray from the camera height towards the direction we are moving
                 Ray forwardRay = new Ray(Camera.main.transform.position, 10* newVector.normalized);
                 Debug.DrawRay(Camera.main.transform.position, 110* newVector.normalized, Color.green, 20, true);
                 if (Physics.Raycast (forwardRay, out hit)) {
                     Debug.Log (hit);
                     //get the obejct hit by ray
                     //new GameObject our_object = hit 
                             
                             //if(our_object's position distance - our position => than our teleport range){
                             // no obstacles
                             // execute teloprt normally
                             // transform.position = transform.position + 10* newVector.normalized;
                             //    }
                                     //else if{obejct's distance - our position < than our teleport range){
                                     //to prevent being stuck in the object
                                     //let's check the width of an object
                                         //if(object.mesh.bounds.size =< for example 100){
                                         //transform.position = transform.position + 10* newVector.normalized;                    
                                         //execute teloprt normally            
                                         //}
 
                                             //else if(object.mesh.bounds.size > 100){
                                             //calculate distance between the object and the player
                                             //thedistance = our_object's position distance - our position
                                             //transform.position = transform.position + thedistance* newVector.normalized;
                                             //this will teleport us directly in front of an object
             
                 }
             }
         
             old_position = transform.position; 
             }
     }
 }





Update: any ideas? :D

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

  • Sort: 
avatar image
1
Best Answer

Answer by Baste · Feb 07, 2015 at 12:19 PM

hit.collider.gameObject will give you the game object hit. Or hit.transform.gameObject.

On finding the width of the hit object, that's harder. But you don't need that. The RaycastHit object has a distance variable, which tells you at what distance the raycast hit the collided with object. The way to do a blink is pretty much:

 void Blink() {
     Vector3 blinkDirection = transform.forward; //or some other, normalized Vector3
     float blinkLength = maxBlinkLength;
     if(Physics.Raycast(transform.position, blinkDirection, out hit, maxBlinkLength) {
         blinkLength = hit.distance;
     }
 
     tranform.position = transform.position + (blinkDirection * blinkLength);
 }

If that finds you stuck in walls, it's because the code above puts the center of your player at where the raycast stops, so your character will be halfway through the wall. In that case, you'll have to subtract half the width of your collider from the raycast distance. Assuming you're using a capsule collider for your character:

 float colliderRadius;
 void Start() {
     colliderRadius = GetComponent<CapsuleCollider>().radius;   
 }

 void Blink() {
     Vector3 blinkDirection = transform.forward; //or some other, normalized Vector3
     float blinkLength = maxBlinkLength;
     if(Physics.Raycast(transform.position, blinkDirection, out hit, maxBlinkLength + colliderRadius) {
         blinkLength = hit.distance - colliderRadius;
     }
 
     tranform.position = transform.position + (blinkDirection * blinkLength);
 }

Hope that helps!

Comment
Add comment · Show 6 · 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 Baste · Feb 07, 2015 at 12:21 PM 1
Share

Note: If you're using some other kind of collider, what you have to do is different. If it's a box collider, assu$$anonymous$$g it's not rotated and has a square bottom, just use half the x- or z- value of it's scale.

If it's a mesh collider, you're doing something seriously wrong.

avatar image Fewpwew130 · Feb 07, 2015 at 12:26 PM 0
Share

Thank you for a prompt answer!!! I will try your code. I am using Character Controller, so I guess it's a capsule collider.

avatar image Fewpwew130 · Feb 07, 2015 at 03:04 PM 0
Share

It works! Thank you very much!!!

avatar image arnesso · May 18, 2015 at 07:29 PM 0
Share

Could you paste here the full teleport script, which is working, please ?Because it has problem with "+" and "-" symbol

avatar image Fewpwew130 · May 18, 2015 at 09:10 PM 0
Share

hey, arnesso!

Sorry, I am a little busy now, I can't remember where the script with blink is.

But as I remember, Baste did an incredible job and his script worked. Could you please specify what is not working?

Show more comments

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

How to teleport the player on which direction its facing? 1 Answer

Blink Ability Issues with Angles and Certain Objects 0 Answers

Bounds IntersectRay distance isnt correct 1 Answer

Get coordonates of a ray in a bounding box? 1 Answer

Get the closest point on a Bounds to a Ray 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