Smooth aim

Hello, i’m trying to do a smooth aim function only with code, animations look tacky in my opinion, i am trying to do it as easy as i can so it doesn’t make my code complex and hard to read and understand, im also trying to keep the codes separate like the mouse look script and one the smooth left to right script and now the aim script but this one is a bit harder to do here is what i came up with but it obviously didn’t work :frowning: please don’t criticize me i’m only just getting in to properly coding my own scripts Here is the code :

var hipToAimSpeed : float;

var hipToAim : Vector3;

function Update () {

if(Input.GetButton(“Fire2”))

hipToAim * hipToAimSpeed;

}

like i said up there Very simple, and wrong :stuck_out_tongue: if you could help that would be great

Thanks

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 =]

Your code currently reads:

If the Fire2 button is pressed then
   calculate the multiplication of the hipToAim vector by the float called hipToAimSpeed

The body of your “if” statement doesn’t look right at all. You are multiplying two variables but not assigning the result to any other variable. It also isn’t clear from the description what it is that you are actually trying to achieve.