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 Frankiej · Jul 21, 2012 at 11:45 PM · cameraraycastfirst person

Raycast and First Person Controller

Hi!

New to game programming and Unity 3D and i am now stuck with my project.

I have a "Plain" on my floor with a blood texture. When my character looks down on it a GUI text will be shown. My idea was to assign this script to the Main Camera in the First Person Controller prefab:

 function Update () {
  var forward = transform.TransformDirection(Vector3.forward); 
  if (Physics.Raycast(tranform.position, forward, 10))
  {
  Debug.Log('Hit');
  }
 }

But it doesn´t work! Complaining about a transform issue. Why? I have draged my script to the Main Camera in the prefab and it has been assigned. So what is wrong? The Main Camera has Transform values so why can´t i access them?

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

2 Replies

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

Answer by ScroodgeM · Jul 22, 2012 at 07:58 AM

  • frist
    transform.TransformDirection(Vector3.forward);
    is same as
    transform.forward;
    • second: didn't you forget assign a collider to plain? raycasts calculates on colliders, not on geometry

    • third: your character i think looks always in horizontal direction. look up and down causes camera to turn, but not character. so when you look down making 'transform.forward' from character's transform will return a horizontal direction

    • fourth: format your code, good formatted questions are more often gets answered

additive edit

using UnityEngine;
using System.Collections;
public class RayCaster : MonoBehaviour
{
    bool iLookAtSomething = false;
    void OnGUI()
    {
        if (iLookAtSomething) { GUI.Label(new Rect(5, 5, 200, 30), "i look at something"); }
    }
    void Update()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit, 10f, -1))
        {
            iLookAtSomething = true;
            //do here whatever you want while looking at something
        }
        else
        {
            iLookAtSomething = false;
        }
    }
}

attach this script to camera and whenever something with collider will be before the camera at minimal distance 10 meters - you got a message on the screen about it

Comment
Add comment · Show 1 · 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 Frankiej · Jul 25, 2012 at 01:32 PM 0
Share

Thx for the code example. $$anonymous$$anaged to solve it by looking at your code and doing it in Javascript with some tweaking.

avatar image
0

Answer by Frankiej · Jul 22, 2012 at 10:02 AM

Thanx for your answer.

  1. Didn´t know that. I´m new my code was from this video: http://www.youtube.com/watch?v=-F_w3RDRbN4&list=PL27B696FB515608D2∈dex=33&feature=plpp_video

  2. Yes, there is no collider on the plain. Didn´t add that because i wanted the raycast to been functional before i did that.

  3. You´re right. That´s why i tried to assign my code to the camera. I´m guessing i have to tweek it.

  4. Did that. Thx for the tip.

Still i´m stuck. How could i solve it. And why does Unity complain about me using just transform? Even if i know i can´t use the transform code to solve my mouse viewing raycast i´m curious why my code doesn´t work at all.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast misses after camera zoom 2 Answers

Camera to aim at raycast 1 Answer

Only show GameObject if at least 1 pixel is rendered 0 Answers

Follow mouse cursor (with Y = 0)? 1 Answer

Raycast to ignore collider ? 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