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
0
Question by RVDL_IT · Apr 20, 2017 at 06:48 PM · cameraraycastpositionupdateboolean

RayCast relative to camera stays in 1 place.

When I start my game the raycast moves with the mouse movement but it stays in the same place where it begins.

 using UnityEngine;
 using System.Collections;
 
 namespace CameraRayCastLine {
 
     public class PickUpRayCast : MonoBehaviour {
         // The camera gameobject.
             public GameObject Camera;
     
     // The camera position and direction.
         // The relative camera position with axis. 
             float CameraPositionX;
             float CameraPositionY;
             float CameraPositionZ;
         // The relative camera direction.
             Vector3 CameraDirection;
         // The camera position in Vector3 form.
             Vector3 CameraPositionVector;
             
     // The RayCast information.
         // The length of the RayCast. 
             public float RayCastLength = 10;
         // The color of the RayCast in the debug.
             public Color CameraRayCastColor = Color.yellow;
         // The bool for the RayCast.
             public bool CameraRayCast;
 
         void Start () {
             // The code that finds the camera.
                 Camera = GameObject.Find("MainCamera");
             // The code that gets the raw camera position axis and converts them to floats.
                 CameraPositionX = Camera.transform.position.x;
                 CameraPositionY = Camera.transform.position.y;
                 CameraPositionZ = Camera.transform.position.z;
             // The code that changes the floats into a refined Vector3.
                 CameraPositionVector = new Vector3 (CameraPositionX, CameraPositionY, CameraPositionZ);
             // The refining of the RayCast.
                 CameraRayCast = Physics.Raycast(CameraPositionVector, CameraDirection, RayCastLength);
         
         }
     
         void Update () {
             // The camera direction refined into Vector3 form.
                 Vector3 CameraDirection = Camera.transform.TransformDirection(Vector3.forward);
             // The actual RayCast.
                 CameraRayCast = true;
             // The debug line that makes the raycast visible in the scene view.
                 Debug.DrawRay(CameraPositionVector, CameraDirection, CameraRayCastColor, RayCastLength);
         }
     }
 }
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 alph1 · Apr 21, 2017 at 08:46 AM 1
Share

You stated that with mouse$$anonymous$$ovement but i dont see any line that contains Input.mousePosition nor a line of similiar reflection.You should transform direction of Input.mousePosition not Vector3.forward.

avatar image RVDL_IT · Apr 21, 2017 at 11:09 AM 0
Share

@alph1 I have the mouse movement in a seperate script. But that's not the problem. The rotation works fine. The position is the problem.

2 Replies

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

Answer by AtGfx · Apr 21, 2017 at 04:36 PM

@RobertVanDerLeeuw

Your problem is that you do not update your CameraPositionVector.

In the Start method you do :

 // The code that changes the floats into a refined Vector3.
 CameraPositionVector = new Vector3 (CameraPositionX, CameraPositionY, CameraPositionZ);

and you do not update this position in the Update method (so it doesn't change, and keep the initial value).

More, I don't really understand why you declare

 // The relative camera position with axis. 
 float CameraPositionX;
 float CameraPositionY;
 float CameraPositionZ;
 // The relative camera direction.
 Vector3 CameraDirection;
 // The camera position in Vector3 form.
 Vector3 CameraPositionVector;

since all the information is contained in the camera itself. I think you could delete these member variables and simply make :

 Debug.DrawRay(Camera.transform.position, Camera.transform.forward, CameraRayCastColor, RayCastLength);
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
avatar image
0

Answer by Potato_Blyat · Apr 21, 2017 at 04:36 PM

im in a hurry, but shortly put, your ray will go forwards in the z axis, from where the camera is at, now i asume you want to have it aim towards the mouse, in which case i would recommend you make a ray variable like so: Ray ray = Camera.main.ScreenToRayPoint(Input.mousePosition); (that's how it goes if memory serves right).

then the ray wil always go from the camera, to your mouse and then you can put in length and all that after.

sorry if this isn't the answer you were looking for but again, in a hurry, best of luck.

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

6 People are following this question.

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

Related Questions

Why is my cursor not moving with the updating position of the raycast hit point? 0 Answers

camera acting weird... 1 Answer

Check if player object is right above this object 1 Answer

Raycast Object Selection 3 Answers

Unity 2D Position Issues 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