- Home /
How to make SwayScript and Pistol Script work together
So whenever I try to play with my sway script and my pistol script, they bug each other out by making the pistol go back and forth a little bit because the pistol script handles the pistols position, and so does the sway script. And I wanna get rid of this problem, making it so that it won't go back and forth in the same place by somehow intergrating the scripts so that they work properly.
Sway Script:
using UnityEngine;
using System.Collections;
public class WeaponSway : MonoBehaviour {
public float mouseSensitivity = 10f;
public float maxMoveAmount = 0.5f;
public float minMoveAmount;
public float smoothSpeed = 3f;
private static Vector3 startPos;
private float factorX;
private float factorY;
public bool moving;
public bool bRotate;
public float smoothRotationSpeed = 2f;
public float rotateAmount = 45f;
//============================================
// FUNCTIONS
//============================================
void Start()
{
startPos = transform.localPosition;
}
void Update()
{
// STORE MOUSE MOVEMENT AMOUNTS, SMOOTH WITH LERP
factorX = Mathf.Lerp(factorX, -Input.GetAxis("Mouse X") * mouseSensitivity, Time.deltaTime * 10f);
factorY = Mathf.Lerp(factorY, -Input.GetAxis("Mouse Y") * mouseSensitivity, Time.deltaTime * 10f);
if(factorX <= minMoveAmount)
{
moving = false;
}
if(factorY <= minMoveAmount)
{
moving = false;
}
if(factorX == maxMoveAmount || factorX > minMoveAmount)
{
moving = true;
}
if(factorY == maxMoveAmount || factorY > minMoveAmount)
{
moving = true;
}
// CLAMP LIMITS
if (factorX > maxMoveAmount)
factorX = maxMoveAmount;
if (factorX < -maxMoveAmount)
factorX = -maxMoveAmount;
if (factorY > maxMoveAmount)
factorY = maxMoveAmount;
if (factorY < -maxMoveAmount)
factorY = -maxMoveAmount;
// SET TARGET POSITION (START POSITION + MOUSE MOVEMENT)
Vector3 targetPos = new Vector3(startPos.x + factorX, startPos.y + factorY, startPos.z);
// APPLY POSITION TO WEAPON, SMOOTH WITH LERP
transform.localPosition = Vector3.Lerp(transform.localPosition, targetPos, smoothSpeed * Time.deltaTime);
// ROTATION
if (bRotate)
{
float tiltAroundZ = Input.GetAxis("Mouse X") * rotateAmount;
float tiltAroundX = Input.GetAxis("Mouse Y") * rotateAmount;
Vector3 target = new Vector3(tiltAroundX, 0f, tiltAroundZ);
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.Euler(target), Time.deltaTime * smoothRotationSpeed);
}
}
}
Pistol Script:
using UnityEngine;
using System.Collections;
public class Pistol : MonoBehaviour {
public string name;
public GameObject obj;
public float damage;
public float accuracy;
public Vector3 defaultPos;
public Vector3 aimPos;
private Vector3 curPos;
private Vector3 nextPos;
public float aimSpeed;
public bool hideMouse;
public bool canMove;
public WeaponSway weaponSway;
void Awake ()
{
nextPos = defaultPos;
if(!weaponSway)
{
Debug.LogError("Assign the weapon sway script!");
}
}
void Update ()
{
if(hideMouse)
{
Screen.lockCursor = true;
}
if(!hideMouse)
{
Screen.lockCursor = false;
}
curPos = obj.transform.localPosition;
obj.transform.localPosition = Vector3.Lerp(curPos, nextPos, aimSpeed);
if(Input.GetMouseButton(1))
{
nextPos = aimPos;
}
if(Input.GetMouseButtonUp(1))
{
nextPos = defaultPos;
}
if(curPos != aimPos && curPos != defaultPos)
{
weaponSway.enabled = false;
canMove = true;
}
if(curPos == aimPos || curPos == defaultPos)
{
weaponSway.enabled = true;
canMove = false;
}
}
}
Comment