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
2
Question by cystemic · Aug 31, 2012 at 02:32 AM · c#rotatelookat

Rotating while rotating?

I'm trying to rotate a gameobject while it's being rotated around a pivot like a canon on a ship that's circling a planet. the canon shouldn't be able to rotate all the way around though, so i've tried using mathf.clamp to limit it but what happens is that it stops facing the center when it rotates. i feel like this is a mathematical problem where i have to calculate and take into account the movement of the ship around the pivot but my brain just isn't making the connection. Any help?

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript1 : MonoBehaviour {
     
     public float horizRotation = 0.0f;
     
     
     void Update () {
           transform.RotateAround (Vector3.zero, Vector3.back, 20 * Time.deltaTime);
         transform.LookAt(Vector3.zero);
 
     
         horizRotation += Input.GetAxis("Horizontal") *100* Time.deltaTime;
         horizRotation = Mathf.Clamp(horizRotation, -45, 45);
         transform.rotation = Quaternion.Euler(0, 0, horizRotation);
 
     }
 }

you should be able to drop the script onto a capsule that's a bit above the centre of the scene and it will orbit 0,0,0. when you press the right or left arrow keys it should rotate the "cannon" but it does it relative to the vertical axis of the screen, not (0,0,0) like it should. Thanks in advance

Comment
Add comment · Show 1
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 Kryptos · Aug 31, 2012 at 07:49 AM 2
Share

The first two rotations (RotateAround and LookAt) are useless since they are both overridden by last statement (transform.rotation).

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Aug 31, 2012 at 04:10 AM

The easiest way is to separate the rotations in two different objects: create an empty object (the container) at the capsule position, and child the capsule to it, then attach the script below to the container (not the capsule!):

using UnityEngine; using System.Collections;

public class ContainerScript : MonoBehaviour {

 public float horizRotation = 0.0f;

 void Update () {
    transform.RotateAround (Vector3.zero, Vector3.back, 20 * Time.deltaTime);
    horizRotation += Input.GetAxis("Horizontal") *100* Time.deltaTime;
    horizRotation = Mathf.Clamp(horizRotation, -45, 45);
    // the capsule is the first (and only) child:
    Transform child = transform.GetChild(0);
    // rotate the capsule relative to its parent:
    child.localRotation = Quaternion.Euler(0, 0, horizRotation);
 }

}

Comment
Add comment · Show 1 · 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 cystemic · Aug 31, 2012 at 04:37 AM 0
Share

It works! thanks so much, I should have thought of that sooner, much easier to attach it to an empty game object than fiddle around with quaternions. :)

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

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

How can I rotate my player head to face a target but only when the target is in the front of the player ? 1 Answer

slow look at question 1 Answer

Distribute terrain in zones 3 Answers

Rotation with multiple objects 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