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 Juri · Oct 17, 2010 at 11:31 AM · physicsraycastai

Physics.Raycast

var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var hunt : boolean = false; var myTransform : Transform; //current transform data of this enemy var distance : int;

function Awake() { myTransform = transform; //cache transform data for easy access/preformance }

function Start() { target = GameObject.FindWithTag("Player").transform; //target the player

}

can somebody tell me how i can make the Raycast work in a way, that my hunter only hunts me if nothing is between us???

function Update () {

 dist = (transform.position - target.position).magnitude;
 if(dist < distance){
     hunt = true;
 }
 else{
     hunt = false;
 }
 var inSight = true;
 if(Physics.Raycast(transform.position,inSight, 5)){
     inSight = false; 
 }

 if(hunt == true && inSight == true){
 //rotate to look at the player
 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

 //move towards the player
 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }

}

Comment
Add comment · Show 1
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 Edy · Oct 17, 2010 at 12:16 PM 0
Share

Which version of Physics.Raycast are you calling? I can't see any match of Physics.Raycast that takes those parameters (http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html)

2 Replies

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

Answer by spinaljack · Oct 17, 2010 at 04:31 PM

You have to understand how the function works. It returns true if anything is intersecting the line that's cast, including the player character. What you need to do is add a second clause that can distinguish between player and non-player objects. You do that using the raycast hit properties.

e.g.

var hit : RaycastHit; var direction : Vector3;

direction = target.position - transform.position;

if (Physics.Raycast (transform.position, direction, hit)) { if(hit.collider.CompareTag("Player")){ // Nothing between the us and the player object // Chase the player }else{ // Something blocking line of sight // Do something else } // Show in the editor the ray Debug.DrawLine (transform.position, hit.point); }

Read the documents carefully about which parameters the various versions use:

function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

EDIT:

use debug draw line to show exactly where the ray is being cast

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 Juri · Oct 18, 2010 at 12:26 PM

alright... so this is cind of working, but I have a big prob, even if there is clearly a Object in the way of my hunter the var InSight is till true.... why is it not false even if a Object is in the way?? here is the Raycast loop like it is right now:

var hit : RaycastHit;
    var direction : Vector3;
    direction =target.position - transform.position;
    if(Physics.Raycast(transform.position , direction, hit)) {
        if(hit == GameObject.FindWithTag("Player")) {
            var InSight = hit.distance;

Comment
Add comment · Show 3 · 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 Bampf · Oct 18, 2010 at 12:42 PM 0
Share

Does the object in the way have a collider on it? If not, the ray won't interact with it. If it does: pause the game at a moment where you think the object is in the way, and look closely at the Scene View. You may find that the ray is just missing the object's collider.. Remember also that the ray is directed from the transform of the hunter towards the player's transform. Typically the transform is at the center, but it doesn't have to be. Again, the scene view can help you figure out what's going on.

avatar image spinaljack · Oct 18, 2010 at 01:04 PM 0
Share

Use comments or edits, don't post new "answers"

avatar image Juri · Oct 18, 2010 at 03:15 PM 0
Share

It has a collider on it but the raycast simply ignores it!!! is there a prob in the code

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

1 Person is following this question.

avatar image

Related Questions

Physics.Raycast AI problem 1 Answer

Raycast problems in my AI 1 Answer

AI Raycast movement and direction 1 Answer

Physics.Raycast not checking layermask properly? 1 Answer

Collision position - Improving on the 'grounded' mechanic 2 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