• 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 Daniel 1 · Jan 24, 2010 at 08:04 PM · physicsiphone

Linecast not working with FPS-prefab?

Im having a curious problem when using Linecast on an enemy (NPC) to detect the Player which is an instance of the "First Person Controls"-prefab (or FPC from now on) in the iPhone Standard Assets.

It will detect the FPC if it is within range and the FPC-object is moved manually within the scene-editor using the mouse. This also changes in real time the values of the FPC's Transform->Position in the Inspector.

But when using the actual controls of the iPhone (via the remote) the FPC is completely ignored by Linecast. Also, the values in the Inspector does not change when moving (Transform->Position).

Ive noticed one curious thing: if the FPC is selected in the Scene-editor and moved by the iPhone via the remote, the center of the FPC will not follow the actual object completely, but stick to a middle point from where the FPC was spawned and where it acually is at any given moment. It's as if the FPC actually got bigger (thus displacing its center) rather than moving.

Another weird thing (which might be a clue for the insighful) is that Raycast does not display the same problems when detecting the FPC.

Anybody have an explanation and preferably also a solution to this weird behaviour?

[code]

var target : Transform;

var smooth : float = 1.0;

function Update ()

{

if( Physics.Linecast( transform.position, target.position ))

{
// TODO: Check distance // Doesnt work when moving player with iPod if( Vector3.Distance( target.position, transform.position ) < 10.0 )

 {
     // Rotate towards player
     var targetPoint = target.transform.position; 
     var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up); 
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0); 

     // Move towards player      
     var targetPosition = Vector3(target.position.x, transform.position.y, target.position.z); 
     transform.position = Vector3.Lerp( transform.position, targetPosition, Time.deltaTime * smooth );
 }

}

}

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 Jaap Kreijkamp · Jan 25, 2010 at 05:31 AM

Have you tried adding debug? Assuming you have set up colliders correctly and the origin of the object with script isn't inside the other object's center the Physics.Linecast should always return true (making the script in current form quite useless I'd say).

What you probably want is to test if there's anything between the enemy and the player character. To check this, there are several ways, one is to have player in its own layer and test if the line from enemy to player hits anything in layers other than player. Another method is to look at the object the line/raycast is colliding with, if this is player there's nothing in between.

Sample how to do this:

var target : Transform; // set to player var layerMask : LayerMask; // set to everything except player layer

function Update() { if (!Physics.Linecast(transform.position, target.position , layerMask.value)) { // player is in line of sight, do your thing here... }
}

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 scarletsnake · Apr 06, 2011 at 04:09 PM 0
Share

Jaap, I cannot begin to express my gratitute. I had been working on a modified version of the AI code included within the FPS tutorial, but somehow the linecasting wasn't working right and I couldn't understand why. Implementing your code and modifying my layers made it work perfectly. Thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Unity Cloth on Mobile? 2 Answers

making perpetual movement 1 Answer

Constant force 2 Answers

Collider Physics Optimization 1 Answer

Gravity not functioning properly? 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