Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by benjimazza · Apr 15, 2012 at 12:16 AM · gameobjectaimsmoothdamp

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image benjimazza · Apr 15, 2012 at 10:09 AM 0
Share

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 ?

avatar image AlucardJay · Apr 15, 2012 at 11:13 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges