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 oliver-jones · May 23, 2011 at 10:33 PM · raycastpositiontarget

A Helping Hand With Raycasting

Hello,

Basically, I have a raycast system that shoots out a raycast directly forward. The problem I'm having is that I don't want it to fire directly straight from the transform. I want it to be able to fire at a target but only on the Y axis.

Here is what I'm using:

     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     var localOffset = transform.position + (transform.up) * adjustRaycast;
     var layerMask = 1 << 10;
     layerMask = ~layerMask;
     if (Physics.Raycast (localOffset, direction, hit, 90000, layerMask) && MachineGun == true) { //direction
          Debug.DrawLine (localOffset, hit.point, Color.cyan);

So now - its firing straight. But I want it to be able to fire straight at a target but be able to move up or down on the Y axis (if the target is higher than the raycast).

Ive tried changing the var direction to:

 var direction = target.transform.position;

But it appears to be firing at the same spot no matter what.

Can someone help me out please.

Cheers

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 Wolfram · May 23, 2011 at 10:41 PM

Your direction needs to be a difference between two vectors, not the target position itself. Try this:

 var positionUsingTargetHeight = transform.position;
 positionUsingTargetHeight.y=target.transform.position.y;
 // now positionUsingTargetHeight contains the starting point you're looking for

 direction = target.transform.position - positionUsingTargetHeight;
   

As a side note, instead of transform.TransformDirection(Vector3.forward) just use transform.forward.

EDIT: Unless I misinterpreted your question. If you want a ray from your current position to the target's position, simply use target.transform as the starting point, and:

 var direction = target.transform.position - transform.position;

as the direction.

Comment
Add comment · Show 8 · 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 oliver-jones · May 23, 2011 at 10:44 PM 0
Share

Humm, thats still not doing it - its just shooting in the same point no matter what

avatar image oliver-jones · May 23, 2011 at 10:50 PM 0
Share

Still not working. Basically - Ins$$anonymous$$d of the raycast just firing forward, I want it to fire forward as well as moving up or down on Y on 'target'.

avatar image Wolfram · May 23, 2011 at 11:08 PM 0
Share

Hm, I'm still not sure I understand your question correctly. Does the player control this up/down movement? Or do you want the ray to always fire straight, but have its height automatically adjusted so that it reaches the target's height if the player is facing the target?

avatar image oliver-jones · May 23, 2011 at 11:10 PM 0
Share

Your second option - thats exactly what I want 'ray to always fire straight, but have its height automatically adjusted so that it reaches the target's height if the player is facing the target'

avatar image Wolfram · May 23, 2011 at 11:15 PM 0
Share

In that case, your starting point would be transform.position, and your direction is:

 var direction = target.transform.position - transform.position;
 direction-=transform.right*Vector3.Dot(direction,transform.right);
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

2 People are following this question.

avatar image avatar image

Related Questions

Raycast not working correct 0 Answers

Lerp isn't completing itself 3 Answers

How to stop a unit from rotation once it gets to designated position? 2 Answers

Getting position where raycast goes offscreen 2 Answers

Object following other object (Parent-like) 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