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 BitWarrior11 · Mar 24, 2019 at 09:33 AM · rotationraycast3dminecraft

Rotate Raycast in 3D Space

Hey, so I've been trying recreate Minecraft in Unity to sharpen my skills, so I've been using Raycasts to detect the 'block' that is front of you. Currently, I am using the Standard Assets FPS Controller with a Ray coming out of the camera. I have been looking everywhere and I cannot find any answers so I apologize if this has already been answered.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BlockManager : MonoBehaviour
 {
     
     public GameObject selectedBlock;
     public Transform player;
 
     private RaycastHit hit;
     private Vector3 RaycastStart;
 
     void Start() {
         RaycastStart = new Vector3(player.position.x, player.position.y, player.position.z);
     }
 
     void FixedUpdate()
     {
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         // Vector3 rotFwd = new Vector3(fwd.x /*player.transform.rotation.eulerAngles.x*/, /*fwd.y*/ player.transform.rotation.eulerAngles.y, /*fwd.z*/ player.transform.rotation.eulerAngles.z);
         RaycastStart = new Vector3(player.position.x, player.position.y, player.position.z);
 
         if (Physics.Raycast(RaycastStart, fwd, out hit, 10) && hit.transform.tag == "DestructableBlock") {
             Debug.Log("There is a block in front of the object.");
             Debug.DrawRay(RaycastStart, fwd * hit.distance, Color.yellow);
         } else {
             Debug.DrawRay(RaycastStart, fwd * 10, Color.yellow);
         }
     }
 
 }

As you can see, selectedBlock is not being set yet, as I am still focusing on the rotation. I need the raycast to point forward in front of the camera (whatever the rotation is) (like in minecraft) to detect any 'block' in front of the camera. I have not been able to find an answer but I feel like its obvious. Thanks!

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
0
Best Answer

Answer by qobion · Mar 24, 2019 at 11:21 AM

You shooting rays in forward direction of your blockmanager gameobject. If this script is not attached to your players transform you should use : Vector3 fwd = player.TransformDirection(Vector3.forward);

Comment
Add comment · Show 13 · 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 BitWarrior11 · Mar 24, 2019 at 12:00 PM 0
Share

This solves my issue about rotation! Thanks! But, one more thing, is it possible to make the raycast rotate up and down as well?

avatar image qobion BitWarrior11 · Mar 24, 2019 at 12:19 PM 0
Share

Sure. Just shoot your ray directly from the camera:

 Transform camera;
         Start ()
         {
             camera = Camera.main.transform;
         }
         
         FixedUpdate()
         {
             Vector3 fwd = camera.TransformDirection(Vector3.forward);
         }
 
avatar image BitWarrior11 qobion · Mar 24, 2019 at 12:29 PM 0
Share

This results in NullReferenceException: Object reference not set to an instance of an object Block$$anonymous$$anager.FixedUpdate () (at Assets/Assets/Scripts/Block$$anonymous$$anager.cs:21)

Show more comments

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

186 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer

Quarternion.LookRotation is only rotating my game object on one axis 1 Answer

How do i get the Raycast rotation? 1 Answer

¿Is it possible to scale independently of the rotation? 0 Answers

3D Heat Seeking Missile (C#) - Odd problem 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