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?
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…
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.
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#
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
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’.
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’.
Assets/Scripts/Camera Scripts/mousePointer.cs(13,66): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected
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