- Home /
weapon bob when looking
Hi!
Until now I only found out how to create a weapon bobbing when moving... But is there a way to make the weapon bob when moving the camera? Like it is a bit too slow?
I think you need to provide more information before you can get a usable answer.
Answer by goo-muffin · Jan 27, 2013 at 11:50 AM
var MovementSpeed :float = 1;
var MovementAmount :float = 1;
var Gun :GameObject;
var MovementX :float;
var MovementY :float;
var newGunPosition :Vector3;
var DefaultPosition :Vector3;
var newGunRotation :Quaternion;
var DefaultRotation :Quaternion;
function Start(){
DefaultPosition = transform.localPosition;
DefaultRotation = transform.rotation;
}
function Update(){
MovementX = Input.GetAxis("Mouse X") * Time.deltaTime * MovementAmount;
MovementY = Input.GetAxis("Mouse Y") * Time.deltaTime * MovementAmount;
newGunPosition = new Vector3(DefaultPosition.x, DefaultPosition.y, DefaultPosition.z);
newGunPosition.x = Mathf.Lerp(DefaultPosition.x, DefaultPosition.x + MovementX, MovementSpeed / 2);
newGunPosition.y = Mathf.Lerp(DefaultPosition.y, DefaultPosition.y + MovementY, MovementSpeed / 2);
newGunRotation = new Quaternion(DefaultRotation.x, DefaultRotation.y, DefaultRotation.z, 1);
newGunRotation.z = Mathf.Lerp(DefaultRotation.z, DefaultRotation.z + MovementX, MovementSpeed);
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, newGunPosition, MovementSpeed * Time.deltaTime);
Gun.transform.localRotation = Quaternion.Lerp(Gun.transform.localRotation, newGunRotation, MovementSpeed * Time.deltaTime);
if(Input.GetKeyDown("escape"))
Application.Quit();
}
Thank you I found it out on my own :D
Answer by schaddem · Jan 26, 2013 at 10:33 AM
You could record the weapons last position and center position(which could also be modified by your walk bob). then you compare newposition with lastposition and base your offset off that. You limit your offset by some value so the weapon always stays within bounds. You move the weapon always towards the centerposition with xspeed, you could use .slerp or so to smoothen the movement.
hope that makes sense