• 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 Barachiel · Aug 05, 2014 at 06:54 AM · raycastanglelimit

Raycast to follow an object on one axis, and another on a different axis

Hi there.

Right now I'm trying to get a raycast to always aim straight ahead of it's origin point, but be able to angle up and down on a single axis depending on where a separate object is located.

I've thought I had it numerous times, but I seem to be pulling up just short of a solution. I'm using DrawLine to see what I'm doing for the moment. Here's my current attempt, with all the irrelevant stuff cut out:

 var weaponFocus =         bulletTarget.transform.position; //This is the object the ray should be following.
 var rayCastPos =          weaponRifle.transform.position;
 var weaponDirection =     weaponRifle.TransformDirection(Vector3.up); //The model I'm using has it's up position at the end of the barrel, so Vector3.up is actually forward. Woops.
 var weaponFocusLimitedX = weaponDirection.x;
 var weaponFocusLimitedY = weaponDirection.y;
 var weaponFocusLimitedZ = weaponDirection.z;
 
 Debug.DrawLine(rayCastPos,Vector3(weaponFocusLimitedX,weaponFocusLimitedY,weaponFocus.z).normalized * 100, Color.red,0.0,true);

The basics of what I'm trying to accomplish here is to have the ray head straight forward from the weapon, but be able to move up and down to follow a target. I don't want it following the target left or right, however.

Any help with this is greatly appreciated.

Comment
Add comment · Show 3
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 Barachiel · Aug 10, 2014 at 03:55 AM 0
Share

I'm still plugging away at this problem on and off between other stuff, and most of my results seem to end in the ray (or the line, in this case) being anywhere from swinging wildly about as soon as I move, to mostly seeming okay except becoming really off center really quickly (occasionally inverting soon after).

I'm seriously at my wit's end on this one and I'm sure it can't be too difficult.

avatar image robertbu · Aug 10, 2014 at 04:17 AM 0
Share

I need more info:

  • Does the character or object holding the gun rotate on any axis 'Y' axis. Said another way, when not tilted up or down like you are trying to implement, does the top of the gun always align with the world up?

  • Is the up/down tilt of the gun driven by user input, or are you trying to make it automatically target something that is up or down from the gun?

  • Have you (or are you planning) to fix the non-standard rotation with respect to what part of the object is forward?

  • I assume this is 3D?

avatar image Barachiel · Aug 10, 2014 at 04:38 AM 0
Share
  • The gun is attached to an animated character, so the top of the gun is rarely lined up perfectly with the world up, though it's always roughly the same.

  • The game is from a pulled back and tilted perspective, not quite top down. At the moment, the gun itself doesn't need to tilt up and down to follow the object, only the ray coming from it. The object will never be so high above the player for this to be an issue. The ray itself does move up and down automatically to follow the object however.

  • I haven't yet fixed the non-standard rotation of the gun as it's been a low priority, but I certainly intend to and will do it sooner if required for this issue.

  • The game is, indeed, in 3D.

As a note, the reason I'm trying to accomplish this is because the character and therefore, the gun, rotate to face a cursor. As is, the ray is pointing at the cursor long before the character and gun has had time to finish rotating to face the same position. With the ray always heading forward from the guns position, this isn't something I need to worry about, but I'd still need the up and down movement of the ray.

Thanks for your response. =)

1 Reply

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

Answer by robertbu · Aug 10, 2014 at 05:15 AM

Okay. Given your description, I'm assuming you are projecting a targeting point into 3D space from the mouse. I'm also going to assume you've fixed your rotation, since dealing with a non-standard rotation is difficult for me to think through.

  • hitPoint - the point projected into 3D space by the mouse position

  • adjustedPoint - a point in front of the gun to use for your targeting

  • adjustedDir - the direction from the gun to the point (may not need)


    var pt : Vector3 = ProjectPointOnPlane(gun.up, gun.position, hitPoint); var fwdDist : float = (pt - gun.position).magnitude; var up : Vector3 = (hitPoint - pt); var adjustedPt : Vector3 = gun.forward * fwdDist + up; var adjustedDir : Vector3 = adjustedPt - gun.position;

    function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 { planeNormal.Normalize(); var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint)); return point + planeNormal * distance; }

This (untested) code works by swinging the targeting point you get from your mouse around so that it is in front of the camera. It gives you a point to aim at or to use in a Linecast() or as a step in creating a direction.

Comment
Add comment · Show 5 · 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 Barachiel · Aug 10, 2014 at 06:43 AM 0
Share

I'm not currently at my work station so I can't test this right away, but that certainly looks like something I can work with.

In order to do a couple of other interesting things, the point I have the ray aimed at is a sphere with the mesh renderer turned off (the renderer only continues to exist for visual debugging now and then). The sphere is a child of an empty game object that follows the mouse position (it also has a plane with some scripts for crosshairs and the like).

Do you foresee any major issues when applying your solution to this situation?

Thanks for your time so far. =)

avatar image robertbu · Aug 10, 2014 at 11:20 AM 0
Share

As long as the aiming point is in 3D space in front of the camera, the code should work. Let me explain conceptually. Imagine there is a plane running through the gun with the plane normal facing up. For example, if the gun up was aligned with the world up, then this would be the XZ plane. Now project the aiming point on plane plane. Next a distance is measured from the gun to the projected point on the plane, and a vector is created from the plane up to the aiming point. Finally we construct a new aiming point that walks out the transform.forward the measured distance, then uses the vector to move up. So the final point will be directly in front of the gun but at right height.

avatar image Barachiel · Aug 11, 2014 at 11:40 AM 0
Share

Hello again. Turns out it's a public holiday today for some local show, but I did run a small test to try out your solution.

Here's the code.

gun$$anonymous$$uzzleObj is just an empty game object representing the muzzle position of the gun, as one would expect.
bulletTarget is a sphere for aiming, in the same sort of setup I have for the crosshair.

 var pt : Vector3 =           ProjectPointOnPlane(gun$$anonymous$$uzzleObj.transform.up, gun$$anonymous$$uzzleObj.transform.position, bulletTarget.transform.position);
 var fwdDist : float =        (pt - gun$$anonymous$$uzzleObj.transform.position).magnitude;
 var up : float =             (bulletTarget.transform.position - pt).magnitude;
 var adjustedPt : Vector3 =   gun$$anonymous$$uzzleObj.transform.forward * (fwdDist + up);
 var adjustedDir : Vector3 =  adjustedPt - gun$$anonymous$$uzzleObj.transform.position;
 
 function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3
 {
     planeNormal.Normalize();
     var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
     return point + planeNormal * distance;
 }


The results seem kind of similar to what I've seen with my own attempts, with it seeming mostly accurate until you start to move around.

This is how it looks just aiming at a pillar/barrel thing from the starting position: alt text

And this is how it looks as I move around and aim at different spots: alt text

alt text

alt text

It seem to try it's hardest to match the target's position vertically (relative to the screenshots), but often falls short. Horizontally, it couldn't care less about.

avatar image robertbu · Aug 11, 2014 at 12:35 PM 0
Share

$$anonymous$$y fault. I had the wrong type on 'up' in my code fragment (now fixed) and confused you. Your line 3 should be:

  var up : Vector3 = bulletTarget.transform.position - pt;

...and then line 4 would be:

  var adjustedPt : Vector3 = gun$$anonymous$$uzzleObj.transform.forward * fwdDist + up;


'up' is the vector from the point on the plane to the target point.

avatar image Barachiel · Aug 11, 2014 at 01:31 PM 0
Share

Ah, there we go, that works much better. I seem to have gone the polar opposite than I should have when fixing that third line, woops. I should have thought to try both.
It aims in the right direction now and doesn't swing wildly about like my own poor attempts, but it seems to be aiming at a point further down and in front of the target than it should be. I'm sorry to keep bugging you, but I feel a little out of my depth here. Looking over the code, based on what I know (and have now learned) I don't see what's causing this.

Here's a couple of screens of what I mean:

alt text

alt text

alt text

alt text

In the 2nd and 3rd screen, I have it set so that I can toggle the target position between two spots (high or low) without moving the cursor itself.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

Raycasting at an offset angle? 4 Answers

How to detect object by rotations-raycast? 0 Answers

Why does the raycast hit only objects/faces of a certain rotation? 1 Answer

Angle between Ray and Normal 1 Answer

How to shoot raycast with slight random direction? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges