- Home /
Sniper Demo: When zoom the raycast change its aim !
Hello everyone..
Im trying to create a sniper demo. Now im in the biggining stage where im focusing on coding the FPS.
Project Specification: Unity FPS Model. I use raycast to aim on collider hit. Camera is a chld object of FPS, camera has two child objects. Bullets and Sniper.
Camera Script
private var baseFOV : float;
function Start () {
baseFOV = Camera.main.fieldOfView;
}
function Update () {
if (Input.GetMouseButton(1)){
Camera.main.fieldOfView = 3;
}else{
Camera.main.fieldOfView = baseFOV;
}
}
Another Script in the camera called CrossHair
var crosshairTexture : Texture2D;
var crosshairTextureZoomed : Texture2D;
static var OriginalOn = true;
public var position : Rect;
function Start()
{
position = Rect((Screen.width - crosshairTexture.width) / 2,
(Screen.height - crosshairTexture.height) /3,
crosshairTexture.width,
crosshairTexture.height);
}
function Update(){
if (Input.GetMouseButton(1)){
position = Rect((Screen.width - crosshairTextureZoomed.width) / 2,
(Screen.height - crosshairTextureZoomed.height) /3,
crosshairTextureZoomed.width,
crosshairTextureZoomed.height);
}else{
position = Rect((Screen.width - crosshairTexture.width) / 2,
(Screen.height - crosshairTexture.height) /3,
crosshairTexture.width,
crosshairTexture.height);
}
}
function OnGUI()
{
if (Input.GetMouseButton(1)){
GUI.DrawTexture(position, crosshairTextureZoomed);
}else{
if(OriginalOn == true)
{
GUI.DrawTexture(position, crosshairTexture);
//yield WaitForSeconds ( 2 );
}
}
}
Bullets Script
Raycast script to shoot.
My Issue.
I have manually centered the Raycast aim to the center of the screen (almost the center)
When i zoom using the right mouse click The Aim of of the camera shoots on the ground. When i go higher and aim it goes higher...
IM I USING THE RIGHT WAY IN CREATING A SNIPER CHARACTER ? If you have a better way or a method please share.
You didn't include the Raycast code so it is difficult to figure out your problem. Note you can construct a ray from the center of the screen by using something like:
var ray = Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0));
Your answer
Follow this Question
Related Questions
Sniper zoom when right click? 1 Answer
How to add sniper zoom effect in an FPS made with Unity3d? 2 Answers
Sniper Scope? 1 Answer