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 Instinct · Feb 21, 2013 at 04:04 AM · c#gameobjectrotate

Help on rotating game object in C sharp

Hello, I'm new to unity (Started with it for two weeks ago).

I have now created a game of a box moving automatically from left to right and small jump function with rigidbody.addforce.

Now, my game should be like this: The player will move to right (without any key input). You should jump over, or duck under obstacles. But, I can't seem to find out how to script the "duck under" mechanic in a good way.

What I had in mind was to rotate my player cube 90 degrees, so it could slide under the obstacle. The player cube is a slim rectangle, which reminds of a human structure. alt text

This is what I have. But doesn't serve as a good mechanic for my game. (it's very clunky, but it rotates it as long as you hold down the button).

 public Vector3 slidePress = new Vector3(0, 0, 90);
     
 void Update () {
 
 if(Input.GetAxis("Horizontal") < 0){
             Quaternion deltaRotation = Quaternion.Euler(slidePress * Time.deltaTime);
             rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
         }
 }


Keep in mind, I'm not very good at syntax in programming, but I do understand the logic of programming. But I find a lot of what I write on the internet. So be gentle on your answers. Thanks :)

illustraion.jpg (48.4 kB)
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

3 Replies

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

Answer by Instinct · Feb 21, 2013 at 06:26 PM

Update: I got somewhere around to what I want with this code (written in java):

 var smooth = 1.0; 
 var duck = 90.0; 
 var stand = 0.0;
 
 function Update () {
  
     if(Input.GetAxis("Vertical") < 0){
     var target = Quaternion.Euler (0,0, duck);
     transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
     }
     else{
     var target2 = Quaternion.Euler (0,0, stand);
     transform.localRotation = Quaternion.Slerp(transform.localRotation, target2, Time.deltaTime * smooth);
     }
 }

If you have any improvement or suggestions then please inform me. Anyhow, thanks for all the help!

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
0

Answer by legion_44 · Feb 21, 2013 at 02:47 PM

Hi, first make Empty GO that will be pivot of your player. Set it position and attach Player Cube as children then attach this script to your pivot:

 public class Rotate : MonoBechaviour {
 void Rotate()
 {
 if(Input.GetKey("c"){
 transform.rotation = Quaternion.Lerp(transform.rotation, /*Here your "duck" rotation*/);
  }
 else{
 transform.rotation = Quaternion.Lerp(transform.rotation, /*Here your standing rotation*/);
   }
  }
 }

Change the /Here your standing rotation/ to Quaternion(x,y,z,w) where x,y,z,w is rotation when player standing. Change the /Here your "duck" rotation/ to Quaternion(x,y,z,w) where x,y,z,w is rotation when player "ducking". sorry if i wried something wrong, i using JS not C# and code is not tested but should work ;)

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
0

Answer by Lockstep · Feb 21, 2013 at 01:54 PM

If you want to do something in a smooth way, you should always consider a Slerp. This could be done like this:

 public float rotatingSpeed = 10f;
 public Vector3 duckEulers;
 private Quaternion duckRotation;
 public Vector3 standEulers;
 private Quaternion standRotation;
 private Quaternion targetRotation;
 
 void Start(){
     duckRotation = Quaternion.Euler(duckEulers);
     standRotation = Quaternion.Euler(standEulers); //probably enough to say = Quaternion.identity
 }
 
 void Update (){
     if(Input.GetKey("DuckKey")){
         targetRotation = duckRotation;
     } else {
         targetRotation = standRotation;
     }
     rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, TargetRotation, Time.deltaTime * rotatingSpeed);  //This will increment the rotation towards the target rotation
 }


Maybe you can also achieve the crounch effect by manipulating the upwards scale.

Comment
Add comment · Show 5 · 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 Instinct · Feb 21, 2013 at 05:48 PM 0
Share

I think your code should be good. But for some reason I get an error saying: "Cannot implicitly convert type 'UnityEngine.Quaternion' to 'UnityEngine.Vector3'"

I think it means that you can't convert Quaternion to Vector3.

Halp pls :)

avatar image Lockstep · Feb 21, 2013 at 06:25 PM 0
Share

Ah sorry. I messed up a bit. targetRotation should be a quaternion but I accidentially declared it as vector. I'll edit it.

avatar image Instinct · Feb 21, 2013 at 07:08 PM 0
Share

Wow, it works perfectly (better than the solution I found!). Just small comment, you have a typo: You have a capital T in your targetRotation at the last line in you code.

Will be using this!

avatar image Instinct · Feb 21, 2013 at 07:15 PM 0
Share

Just a small question, after my player cube rotates it kinda gets submerged into the platform cube (the ground).

This causes problems for me, as the [pseudo] if(touchGround){ /JU$$anonymous$$P code here / } doesn't work properly (the game doesn't see that I'm actually touching the platform).

Got any workarounds or tips for me?

(I have the if(touchGround){jump} so I can't jump while im in the air).

avatar image Lockstep · Feb 21, 2013 at 08:20 PM 0
Share

This happens because manipulating rigidbody.rotation overrides PhysiX, thus ignoring collisions. You would need to use rigidbody.AddTorque if you want to prevent that. Although this might come with other inconveniences like bouncing in an unwanted angle.

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

11 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

Related Questions

How do I follow a gameobject with the camera 3 Answers

How to destroy object forever? when i go to another scene and back to first, i want to that object stay destroyed! , Please !!!!  1 Answer

Rotate GameObject Issue 1 Answer

How to find all GameObjects in a parent in shorter code than I figured out 1 Answer

Camera rotation around player while following. 6 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