- Home /
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 :( 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 :p if you could help that would be great
Thanks
Answer by AlucardJay · Apr 15, 2012 at 11:22 AM
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 =]
Answer by kolban · Apr 15, 2012 at 01:36 AM
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.
im not trying to argue, but how isnt it clear what im trying to achieve to me it says what im trying to in the first 11 words :/ im not a coder but people always piss and moan that others wont do the code for them therefore i had a go at my own code before posting this can blame a guy for trying right ?
while avoiding this conflict I just want to say, kolban is pointing out that the command
hipToAim * hipToAimSpeed;
will not actually do anything. I am just pointing this out so you can see and understand this idea.
You want 3 things from this command. Currently you have a position you want to move to, and the speed to move at. But you need to put this information to something to make it work.
makeObjectPosition = hipToAim * hipToAimSpeed;
Now you have a position you want the object to be , you can use this for example
transform.position = makeObjectPosition;
Ok , so that's the theory of the comment. Now, to help you , I have written some code to demonstrate how you can do this. See my answer.
Your answer
Follow this Question
Related Questions
How to make an object appear after collecting items? 3 Answers
Cant access a gameobject's position 1 Answer
creating a GameObject Instead of a Cube 1 Answer
Having a child gameobject reside within restricted cooridinates? 0 Answers
Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject' 1 Answer