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 Cow dude · Jul 21, 2013 at 06:30 AM · raycastarray

How to write raycast arrays?

So i so this video on youtube.com and i was wondering how to do this. i am trying to make a 3rd person game but my camera keeps going through the wall even when i use a raycast, but this video with the arrays makes it work perfect. i was wondering if you could post a script on how to do this or a link to a good tutorial on how to do this myself. thanks for all the help

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 robertbu · Jul 24, 2013 at 02:29 AM 0
Share

I missed what you were asking, so I deleted my answer. I can only guess at what they are doing. I think they are casting five rays parallel to each other. If any hit, they use the closest distance to the character. This way the Lerp() starts earlier and has less likelihood of clipping the geometry.

avatar image Cow dude · Jul 24, 2013 at 02:59 AM 0
Share

do you know how one would script that?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 24, 2013 at 04:18 AM

Looking at the video, I'm not sure what they are doing. I would have though they would raycast the full (ideal) distance to the camera, but it looks like they only cast part of the way. Here is a script along the lines of what I think they are doing.

 var player : Transform;
 var dist = 10.0;    // Ideal camera distance
 var castDist = 4.0; // Amount to check
 var speed = 3.0;    // Speed for camera movement
 var castOffset = 0.8;
 var myTrans;
 var offsets : Vector3[] = new Vector3[5];
  
 function Start() {
     offsets[0] = Vector3.zero;
     offsets[1] = Vector3.up    * castOffset;
     offsets[2] = Vector3.down  * castOffset;
     offsets[3] = Vector3.left  * castOffset;
     offsets[4] = Vector3.right * castOffset;
     myTrans = transform;
 }
  
 function LateUpdate() {
     var hit : RaycastHit;
     var pos : Vector3;
     var dir = transform.position - player.position;
     
     pos = player.position + dir.normalized * dist;
     
     var curDist = dist;
  
      for (i = 0; i < offsets.Length; i++) {
     
         if (Physics.Raycast(player.position + myTrans.TransformDirection(offsets[i]), dir, hit, castDist)) {
             var temp = (hit.point - player.position).magnitude;
             if (temp < curDist) {
                 pos = hit.point;
                 curDist = temp;
             } 
         }
     }
  
     transform.position = Vector3.Lerp(transform.position, pos, speed * Time.deltaTime);
     transform.LookAt(player.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

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

Change multiple object layers 0 Answers

Only one raycast out of two works as intended 2 Answers

How to store inspector values of script into array for later use? 1 Answer

How to move like Pacman/Snake using Raycasts inside a 2D array ? 1 Answer

Add object on trigger to array, then cast a ray at all objects in the array 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