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 AidanHorton · Sep 01, 2014 at 02:42 PM · gamefollowsmooth

RayCasting from an object and SmoothFollow

Main problem

I am making a space game styled like the game 'Rodina' at the moment, and it is mainly going well; however I am changing the control system to control better using raycasting, though I have run into a few problems. I have the code for a script here:

 var hit : RaycastHit;
 
 function Update () {
     var ray = Camera.main.ScreenPointToRay(Vector3.forward);
     if (Physics.Raycast(ray, hit)) {
         Debug.DrawLine(ray.origin, ray.direction, Color.green);
     }
 }



This script is attached to the ship object. However, when I execute the script this happens:alt text

The 'Debug.DrawLine' may be the problem, but as you can see, the line drawn goes almost the exact opposite way intended. Any ideas why this might be? I can post the main code if needed, as the controls for the ship also use the camera by using 'Camera.main.ScreenToWorldPoint', which could be interfering.

Other Problems

These are not really connected to the question asked, but I may as well ask them here so I dont clog up Unity Answers. The Unity script 'SmoothFollow' is good in all, but it doesn't allow proper rotations, and it seems to bug out my controls a little, as it is based on the way the camera moves with the ship. It's hard to explain, so I have pictures instead:

alt text

As you can see, the camera doesn't follow behind it correctly, the same is also true for banking upwards. The intended follow would look like the first image on the right-side, which is following at the right height behind the ship, whilst pointing up or down. So if anyone could help with a better way of going about the camera in order to accurately follow my ship that would be appreciated.

If needed I can post the main controls for the ship, as it may be interfering

raycastproblem.jpg (172.2 kB)
smoothfollowbug.jpg (120.8 kB)
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
1
Best Answer

Answer by robertbu · Sep 01, 2014 at 03:01 PM

You don't say what your Raycast() should be doing, so it is hard to give an accurate answer. But I can tell you that what you have now is wrong. Vector3.forward is a direction, not a position and is (0,0,1). Screen coordinates start in the lower left of the screen at (0,0), so your code is casting a ray from the lower left corner of the screen. My guess is that you want to Raycast from the center of the screen. Here are a few different ways of doint that:

  Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, hit);

---

  var ray = Camera.main.ViewportPointToRay(Vector2(0.5, 0.5));
  Physics.Raycast(ray, hit);


 var ray = Camera.main.ScreenPointToRay(Screen.width / 2.0, Screen.height / 2.0);
 Physics.Raycast(ray, hit);


As for the SmoothFollow(), 1) that is a separate issue and should and 2) as someone who answers a lot of that kind of question, I don't understand the question well enough to answer. You need to give more of a description and include your code.

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 AidanHorton · Sep 01, 2014 at 04:56 PM 0
Share

Thanks so much that was very helpful! And it worked perfectly. I actually needed it to come from the ship itself, but I modified the first method in order to facilitate that. Thanks!

avatar image AidanHorton · Sep 01, 2014 at 05:18 PM 0
Share

Also as a side note, I have just submitted a question about the Unity SmoothFollow script, be sure to look at it when it is approved as I really need help. Thanks!

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

2 People are following this question.

avatar image avatar image

Related Questions

SmoothFollow Unity Script improvements? 1 Answer

HI, how to smoothly rotate 2d topdown if dist is less than x.code below? Thanks and if someone would want to help i'd like code.thanks a bunch 0 Answers

Texture following cursor... 1 Answer

Smooth Follow Camera shakes violently 2 Answers

C # script error CS1729. 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