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 Lordshmuck666 · Feb 21, 2014 at 08:29 PM · randomtimershootingtargetrotating

How can I rotate a shooting-practice target 180 degrees with a timer in C#?

I'm trying to create a shooting range with targets that pop out from the ceiling at random. Preferably, they would rotate sideways from their initial position [UP] (from 0 to 180 degrees) and then back again after spending 4 seconds in the end position [DOWN].

If hit by the 'bullet' or raycast ray, they should return to their initial position and then they will come out again if chosen by the script (which I also need help with).

Any ideas/tips/assistance will be much appreciated.

P.S. I'm quite green when it comes to programming in ALL languages and I'm starting off with C# due to the course I'm taking.

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
0

Answer by robertbu · Feb 21, 2014 at 09:18 PM

There are a huge number of ways of implementing this functionality in Unity. One way is to use an AnimationCurve. An animation curve would allow you to give your object a little more life...i.e. it could swing back an forth a bit at the end of its swing so that it felt more real. Attach this script to the object you want to rotate. Note you want the pivot point to be at the bottom of the object. You can accomplish this by putting an empty game object at the bottom of the visible object and making the visible object a child. The script would go on the parent. Here is a sample script. Hit the space bar to cause the object to swing:

 #pragma strict
 
 var ac : AnimationCurve;
 private var timer = 100.0;  // Beyond end of curve
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.Space)) {
         timer = 0.0;
     }
 
     timer += Time.deltaTime / 5.0;
     transform.eulerAngles = Vector3(ac.Evaluate(timer)*180.0, 0.0, 0.0);
 }

Once you've attached the script, you can click on the animation curve and create a curve to match your object. For example:

alt text

A value of 0.0 is rotation 0.0, a value of 1.0 is 180 degrees. The horizontal values go from 0.0 to 1.0. In the script I divide by 5.0, so the whole curve will take 5.0 seconds to execute and be down for approximate 4.0 seconds. Note the wiggle when the graph reaches 1.0. This will cause the object to swing a bit when it first rotates down.


curve.png (23.5 kB)
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

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

18 People are following this question.

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

Related Questions

How can I get my 2D enemy to rotate to look at my player? 2 Answers

choosing between different targets 1 Answer

shooting gun help 0 Answers

What am I doing wrong with shooting at mose position? 1 Answer

Cannot convert `lambda expression' to non-delegate type `bool' 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