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
0
Question by ragnaros100 · Jun 27, 2012 at 10:55 AM · c#animationpositionfpsshooter

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 :)

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

1 Reply

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

Answer by AlucardJay · Jun 27, 2012 at 11:55 AM

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

  • http://answers.unity3d.com/questions/240273/smooth-aim.html

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 );
         }
     }
     
 }


Comment
Add comment · Show 4 · 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 ragnaros100 · Jun 27, 2012 at 12:17 PM 0
Share

Thanks :D... That looks awesome... I'll try to make something in C# :D...

avatar image AlucardJay · Jun 27, 2012 at 12:52 PM 0
Share

I have just updated my answer with a C# version, let me know if there are any problems or conversion errors I missed =]

Just play around with the hipToAimPosition values until the model is going to the right place looking from the camera perspective.

avatar image ragnaros100 · Jun 27, 2012 at 02:43 PM 0
Share

Thanks. It helped alot! :D... There's only some $$anonymous$$or bugs that i can easilly fix :D

avatar image AlucardJay · Jun 27, 2012 at 02:56 PM 0
Share

I shall properly run the C# code in a scene and fix it for future use. Glad it helped =]

Tested and working script is now on the 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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Switching between gun positions 1 Answer

How to animate RectTransform change in position 1 Answer

Unity 3 Animation Positioning Problem 1 Answer

Multiple Cars not working 1 Answer

Animate child of FPSController 0 Answers


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