3rd Person- crosshair to follow where gun aims to-

I used the C# code in the unity community site- with a custom texture and it works perfectly- but it follows the mouse which is connected to the gun with a mouse look- but the mouse and gun are not callobrated to be exact-

If I’m testing- the mouse has to move way off the screen to get the gun to move to the edge of the screen- is this a damp issue or is there something I can do to callobrate them to be right on eachother?

I’m not sure exactly what your are trying to do. It sounds like a target range kind of game? Like you move the mouse on the screen and the gun points at the mouse, as apposed to an FPS where moving the mouse rotates the camera?
I think a useful function that would help you out is Camera.ScreenPointToRay and then use the returned ray in a raycast function to test for walls, target, or enemies and then use a LookRotation on the gun to point it at the point of collision on that ray.
Let me know if that made any sense.

Well- say I start my test build of the game as soon as it starts the crosshair starts in game in whatever position it was when I started the game not inline with the gun tube- so the crosshair is off- How do I get the crosshair/cursor to start ingame where the guntube is pointing?

Oh, I see, it’s a 3rd person shooter. Is your cross hair on the GUI?

I’ve tryied it on a few different places- I don’t have it anywhere right now because it doesnt work- Not sure how you go about doing this and there is no documentation on it- as far as I know or have seen- I searched for hours…

I just noticed your in Mich. Where at??

I have this c# code attached to a GUITexure-

using UnityEngine;

 using System.Collections;

 

public class mousePointer : MonoBehaviour

{

     public Texture2D cursorImage;

 

    private int cursorWidth = 32;

     private int cursorHeight = 32;

 

    void Start()

     {

         Screen.showCursor = false;

     }

 



    void OnGUI()

     {

         GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);

     }

	

	

}

It works good but just isn’t connected to the gun tube-

In short he wants the gun to move along with the mouse movement. Or to follow the Crosshairs. Go and look at the 3rd Person shooter demo. They do implement this into there game. However I don’t think there is any easy way to do this. Since it has to move the bones of the player that your using and from my experience anything with movement in 3d space usually isn’t easy to complete.

If anyone has some amazing tutorials on how movement works in 3d post them. Would help many of us. The Quaternions are what throw me off the most.

You gotta link of this 3rd person demo???

A screenshot from your game would be helpful in this case since the issue is on graphics, but I’ll give it a shot.

You have a point in 3D space (the gun tube) and as the tank moves about, you want the gun tube’s crosshair to follow the gun tube’s rotation.

Simple enough. We will make use of World to Screenpoint coordinates.

This method reads Vector3 coordinates from our level (x,y,z), and translates them to Vector2 coordinates on our screen (x,y).

Make an empty gameObject and make it a child of the gun tube. Position the child just in front of the gun tube, so the crosshair displays in front of the gun tube and not ON the gun tube.

On the child attach this code :

using UnityEngine;
using System.Collections;

public class mousePointer : MonoBehaviour {

public Texture2D cursorImage;
private int cursorWidth = 32;
private int cursorHeight = 32;
private Transform myTransform;
private Camera myCamera;
 
void Start() {
   myCamera = gameObject.FindWithTag("MainCamera").GetComponent(Camera);
   myTransform = transform;    //So you don't GetComponent your transform with every OnGUI call
   Screen.showCursor = false;

}

 
void OnGUI() {

    Vector3 screenPos = myCamera.WorldToScreenPoint(myTransform.position);
    screenPos.y = screen.height - screenPos.y; //The y coordinate on screenPos is inverted so we need to set it straight
    GUI.DrawTexture(new Rect(screenPos.x, screenPos.y, cursorWidth, cursorHeight), cursorImage);
}
}

Haven’t tested it but it should produce the effect you’re looking for.

// this script should make the gun point at where the mouse is currently positioned above
function Update ()
{
var aimPoint : Vector3;
var closestDistFromCamera : float = Mathf.Infinity;
var hits : RaycastHit[];
hits = Physics.RaycastAll(ScreenPointToRay(Input.mousePosition));
for (var i = 0;i < hits.Length; i++) {
    var hit : RaycastHit = hits[i];
    if (Vector3.Distance(hit.point, ScreenPointToRay(Input.mousePosition).origin) < closestDistFromCamera){
          aimPoint = hit.point;
          closestDistFromCamera = Vector3.Distance(hit.point, ScreenPointToRay(Input.mousePosition).origin);}
}
gun.transform.rotation = Quaternion.LookRotation(aimPoint - transform.position);
}

This code is in Java, but the same basic concept would work in c#

Diviner

I got two errors on that C#-

Assets/Scripts/Camera Scripts/mousePointer.cs(13,26): error CS0176: Static member `UnityEngine.GameObject.FindWithTag(string)’ cannot be accessed with an instance reference, qualify it with a type name instead

(Filename: Assets/Scripts/Camera Scripts/mousePointer.cs Line: 13)

Assets/Scripts/Camera Scripts/mousePointer.cs(23,19): error CS0103: The name `screen’ does not exist in the current context

(Filename: Assets/Scripts/Camera Scripts/mousePointer.cs Line: 23)

Here is a screenshot of the scene-

ET-

Here are the errors throw by the .js-

Not sure if I am suppose to have the naming conventions the same in places-

Assets/Scripts/Camera Scripts/AimPoint.js(7,27): BCE0005: Unknown identifier: ‘ScreenPointToRay’.

(Filename: Assets/Scripts/Camera Scripts/AimPoint.js Line: 7)

Error: Analytics Event: 5(CompilerBCE0005Unknown identifier: ‘ScreenPointToRay’.)(1): skipped because it was sent more than once in 0.10 seconds
Assets/Scripts/Camera Scripts/AimPoint.js(10,37): BCE0005: Unknown identifier: ‘ScreenPointToRay’.

(Filename: Assets/Scripts/Camera Scripts/AimPoint.js Line: 10)

Error: Analytics Event: 5(CompilerBCE0005Unknown identifier: ‘ScreenPointToRay’.)(1): skipped because it was sent more than once in 0.10 seconds
Assets/Scripts/Camera Scripts/AimPoint.js(12,63): BCE0005: Unknown identifier: ‘ScreenPointToRay’.

(Filename: Assets/Scripts/Camera Scripts/AimPoint.js Line: 12)

Assets/Scripts/Camera Scripts/AimPoint.js(14,1): BCE0005: Unknown identifier: ‘gun’.

(Filename: Assets/Scripts/Camera Scripts/AimPoint.js Line: 14)

Sorry about that. Told you the code wasn’t tested. It’s two typos I did (didn’t capitalize two words).

Change

myCamera = gameObject.FindWithTag(“MainCamera”).GetComponent(Camera);

to

myCamera = GameObject.FindWithTag(“MainCamera”).GetComponent(Camera);

and

screenPos.y = screen.height - screenPos.y;

to

screenPos.y = Screen.height - screenPos.y;

That should take care of the errors.

Testing it right now I will let you know-

I really appreciate the time your taking to help-

Diviner

More errors-

Assets/Scripts/Camera Scripts/mousePointer.cs(13,66): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

(Filename: Assets/Scripts/Camera Scripts/mousePointer.cs Line: 13)

Assets/Scripts/Camera Scripts/mousePointer.cs(13,52): error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)’ has some invalid arguments

(Filename: Assets/Scripts/Camera Scripts/mousePointer.cs Line: 13)

Assets/Scripts/Camera Scripts/mousePointer.cs(13,52): error CS1503: Argument #1' cannot convert object’ expression to type `System.Type’

(Filename: Assets/Scripts/Camera Scripts/mousePointer.cs Line: 13)

Change

myCamera = GameObject.FindWithTag("MainCamera").GetComponent( Camera);

to

myCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();

I usually script in Unityscript so it’s easy for me to miss things like that when I go back to C#.

You are awe and some buddy-

That is perfect- just alittle adjusting and it will be the cats arse-

Now this should be a sticky-

Now- Do you know anything about 120mm Sabot round ballistics…LOL

Actually I was a Sergeant in the Greek Army in Tank Division, so I know a thing or two about 120mm rounds.

We got L2A4 and L2A6 over here :slight_smile:

I was a Corp. in the U.S. Army in the 2nd Armored Division- HellOnWheels- Gunner-