animations or code?

Hi.
I have this shootergame and I want to make a function when you press the right mouse button you aim down your sights of the current gun equipped. I’ve tried to use animations to make the gun fit the preffered position, but I cant seem to get the coordinates right. Isnt there really a more precise way to do this? (I mean animate with code) If so. How?
(I use C#)

-thanks :slight_smile:

There should be lots of information of you search this 'site ! Here’s an answer I did in JS, I shall check later and see if you need help with converting it, or have found/received a better answer.

From

As I understand it, you want to raise the object while holding the right mouse button , then return the object to the normal position when the right mouse button is released. Here is some code I wrote , with lots of comments to help explain the steps being taken. Make a new scene , add a cube , then attach this script to see what is happening. (also , try rotating the object to different angles to see how this is working in local space , not world space) :

#pragma strict

private var hipToAimPosition : Vector3; // where you want the object to move to 
private var normalPosition : Vector3; // where the object is normally
var hipToAimSpeed : float = 5.0; // how fast the object moves

function Start() 
{
	normalPosition = transform.position; // store the normal position of the object
}

function Update() 
{		
	// Move object depending on RMB state
	if(Input.GetMouseButton(1)) // checks the RMB is down each frame i.e. held down
	{    		
		// set position where object goes to
		// transform.forward + transform.up + transform left (-right)
		hipToAimPosition = transform.forward * 5; // move object forward by 5 units
		hipToAimPosition += transform.up * 5; // move object up by 5 units . *notice the +=
		hipToAimPosition += -transform.right * 2; // move object left by 2 units (-right = left) . *notice the +=
		// change the values (5,5,2) to the values you need to move the object to the correct position
			
		// this prints out in the console the position the object is moving to.
		Debug.Log("hipToAimPosition = " + hipToAimPosition);
	
		// move object to target position over time * smooth
		transform.position = Vector3.Lerp (transform.position, hipToAimPosition, Time.deltaTime * hipToAimSpeed );
	}
	else
	{
		// return object to original normal position over time * smooth
		transform.position = Vector3.Lerp (transform.position, normalPosition, Time.deltaTime * hipToAimSpeed );
	}
}

from the Unity Scripting Reference : http://unity3d.com/support/documentation/ScriptReference/Vector3.Lerp.html

hope this helps =]

EDIT : This is a tested working version of a C# version. Create a cube, then attach this script (call the script LerpAim) :

using UnityEngine;
using System.Collections;

public class LerpAim : MonoBehaviour {

	private Vector3 hipToAimPosition; // where you want the object to move to 
	private Vector3 normalPosition; // where the object is normally
	public float hipToAimSpeed = 5.0f; // how fast the object moves
	
	void Start() 
	{
	    normalPosition = transform.position; // store the normal position of the object
	}
	
	void Update() 
	{     
	    // Move object depending on RMB state
	    if (Input.GetMouseButton(1)) // checks the RMB is down each frame i.e. held down
	    {       
	       // set position where object goes to
	       // transform.forward + transform.up + transform left (-right)
	       hipToAimPosition = transform.forward * 5.0f; // move object forward by 5 units
	       hipToAimPosition += transform.up * 5.0f; // move object up by 5 units . *notice the +=
	       hipToAimPosition += -transform.right * 2.0f; // move object left by 2 units (-right = left) . *notice the +=
	       // change the values (5,5,2) to the values you need to move the object to the correct position
	
	       // this prints out in the console the position the object is moving to.
	       Debug.Log("hipToAimPosition = " + hipToAimPosition);
	
	       // move object to target position over time * smooth
	       transform.position = Vector3.Lerp (transform.position, hipToAimPosition, Time.deltaTime * hipToAimSpeed );
	    }
	    else
	    {
	       // return object to original normal position over time * smooth
	       transform.position = Vector3.Lerp (transform.position, normalPosition, Time.deltaTime * hipToAimSpeed );
	    }
	}
	
}