- Home /
Aimimg guns like in reciever
How do I aim my gun like in the game reciever. In receiver the gun when aimed moves independently of the camera inside a small deadzone when it reaches past that deadzone the camera rotates in the direction the gun is being moved. https://www.youtube.com/watch?v=GCThInmzjXw
I was able to get the gun to move within a deadzone but when it leaves the deadzone the gun does properly rotate with the camera. Instead the distance between the camera and gun is changed and the gun becomes unusable.
My code:
public class GunMouseLook : MonoBehaviour {
public Transform gun,gunRest,gunAim;
public float aimSpeed;
public float XGunAimSensistivity = 3f;
public float YGunAimSensistivity = 3F;
public SimpleSmoothMouseLook ssml;
[Range(0,1)]
public float xMaxDist;
[Range(0,1)]
public float yMaxDist;
[Range(0,1)]
public float zMaxDist;
[Range(0,1)]
public float zMinDist;
[Range(0,1)]
public float zInterval;
public float maxDistanceFromCenter;
public float maxDist;
bool reachedTarget;
// Update is called once per frame
public void LateUpdate()
{
if(Input.GetMouseButton(1)){
GunLook();
}
else{
NormalLook();
}
}
public void NormalLook(){
Cursor.lockState = CursorLockMode.Locked;
gun.position = Vector3.Lerp(gun.position,gunRest.position,aimSpeed*Time.deltaTime);
gun.rotation = Quaternion.Slerp(gun.rotation,gunRest.rotation,aimSpeed*Time.deltaTime);
reachedTarget = false;
ssml.enabled=true;
}
public void GunLook(){
if(!reachedTarget){
if((Vector3.Distance(gun.position,gunAim.position)<0.1f)){
reachedTarget=true;
} else{
gun.position = Vector3.Lerp(gun.position,gunAim.position,aimSpeed*Time.deltaTime);
gun.rotation = Quaternion.Slerp(gun.rotation,gunAim.rotation,aimSpeed*Time.deltaTime);
}
return;
}
Vector3 screenPointGun = Camera.main.WorldToScreenPoint(gun.position);
float distanceFromCenter = Vector3.Distance(screenPointGun,new Vector3(1/2,1/2,0));
Debug.Log(distanceFromCenter);
if(distanceFromCenter > maxDistanceFromCenter)
ssml.enabled = true;
else{
float z = Mathf.Clamp(gun.position.z + zInterval * Input.mouseScrollDelta.y, zMinDist, zMaxDist);
Vector2 mp = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * 0.1f;
gun.position = (gun.position + new Vector3(mp.x, mp.y, 0));
gun.rotation = transform.rotation;
}
}
}
you are adding to the gunposition the inputaxis value, when the distanceFromCenter is lower than the maxDistanceFromCenter, shouldnt it be the other way? you should be adding the value while your distanceFromCenter is higher or i am missing something?
Got any examples of your issue, images, code. more details
$$anonymous$$e and my Camera manipulation is not so hot.. Bump.
Answer by Cornelis-de-Jager · Feb 24, 2019 at 11:08 PM
The function below checks for a deadZone angle and rotates the transform if the angle is past:
public float deadZone = 30;
void GunLookDeadZoneRotation() {
// Get the angle
float angleDiff = Vector3.Angle (transform.forward, gunAim.forward);
float rotY = angleDiff.y;
if (angleDiff < deadZone)
return;
transform.rotation = transform.localRotation * Quaternion.Euler(-rotY, 0f, 0f);
}