Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 herano · Sep 14, 2016 at 03:48 AM · transform.positionrotate objectrotatearoundinput.getaxis

RotateAround object around player when pushing button.

Hello,

Im trying to write a code where a blueCube rotates around a redCube horizontally when pushing the left and right arrow keys and vertically when pushing the up en down keys. neither of them have rigidbodies. I created the code down below, and it seems to work fine if I move only in a horizontal or on a vertical line. But If I combine them The blueCube does not fully rotate around the redCube, but makes circles beside it or rotates round itsself (hopefully you understand what I mean, kinda hard to explain^^). So my questions are:

a. How can I make it that the rotation does work like the blueCube Rotates the redCube the right way?

b. How can I make it so that the blueCube can not sink in the ground when moving it vertically (the -y axis relative to the position of the redCube)?

Many Thanks for helping me out!

My code:

 using UnityEngine;
 using System.Collections;
 
 public class BlueCubeMovement : MonoBehaviour {
     public Transform redCube;
     public float moveSpeed;
     private float inputVertical;
     private float inputHorizontal;
     private float movementVertival;
     private float movementHorizontal;
 
     void Update ()
     {
         float inputHorizontal = Input.GetAxis("RotateHorizontalBlue");
         float movementHorizontal = inputHorizontal * moveSpeed * Time.deltaTime;
         transform.RotateAround(redCube.position, Vector3.up, movementHorizontal);
 
         float inputVertical = Input.GetAxis("RotateVerticalBlue");
         float movementVertical = inputVertical * moveSpeed * Time.deltaTime;
         transform.RotateAround(redCube.position, Vector3.forward, movementVertical);
     }
 }

  
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

Answer by iabulko · Sep 14, 2016 at 06:11 AM

a. If you want to rotate vertically, you should use blue cube's local x axis, not global. Like this:

 transform.RotateAround(redCube.position, transform.right, movementVertical);

b. I don't know if i understand you correctly. To make it not sink into the ground just check it's Y position and make sure it's not lower than ground (i assume that ground Y position is 0). Type this at the end of Update() function:

 if(transform.position.y < 0)
             transform.position = new Vector3(transform.position.x, 0, transform.position.z);


Comment
Add comment · Show 3 · 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 herano · Sep 14, 2016 at 01:36 PM 0
Share

Hey aibulko, Thanks for the answers! Im running in some new trouble though.

a. With this code the blueCube Rotates auround its own axis, not around the redCube. Is there any way I can do this? I tried adding redCube.transform.right, but this does not do the trick.

b. This does prevent the cube from going lower than the floor, but the blueCube moves wacky after that, going closer and farther away from the redCube, while I want it to maintain the same distance.

I will try to learn how to make a GIF soon, because explaining is not really a strong point of $$anonymous$$e^^.

avatar image iabulko herano · Sep 14, 2016 at 03:07 PM 0
Share

You can always record video and post it on one of the video websites ;>. Anyway, this is What I ended with:

 using UnityEngine;
 using System.Collections;
 
 public class BlueCube$$anonymous$$ovement : $$anonymous$$onoBehaviour {
     public Transform redCube;
     public float moveSpeed;
     private float inputVertical;
     private float inputHorizontal;
     private float movementVertival;
     private float movementHorizontal;
 
     private Vector3 beforeSinkingPosition;
     private Quaternion beforeSinkingRotation;
 
     void Start()
     {
         beforeSinkingPosition = transform.position;
         beforeSinkingRotation = transform.localRotation;
     }
 
     void Update ()
     {
         float inputVertical = Input.GetAxis("RotateVerticalBlue");
         float movementVertical = inputVertical * moveSpeed * Time.deltaTime;
         transform.RotateAround(redCube.position, transform.forward, movementVertical);
 
         if (transform.position.y < 0) {
             transform.position = beforeSinkingPosition;
             transform.localRotation = beforeSinkingRotation;
         }
 
         float inputHorizontal = Input.GetAxis("RotateHorizontalBlue");
         float movementHorizontal = inputHorizontal * moveSpeed * Time.deltaTime;
         transform.RotateAround(redCube.position, Vector3.up, movementHorizontal);
 
         if(transform.position.y >= 0){
             beforeSinkingPosition = transform.position;
             beforeSinkingRotation = transform.localRotation;
         }
     }
 }
avatar image herano iabulko · Sep 15, 2016 at 06:03 AM 0
Share

Hey Iabulko, Thanks again! I tried to play around a bit with your code, but it seems the blue cube still goes into the ground and gets struck there sometimes. I wonder if 0 is calculated from the core of the cube, not the sides. I will try it a bit more, but honestly I think I am doing something that is to complicated for a unity noob like me:). I think I will leave the vertical movement for what it is and just use the horizontal. I think I can make a fun first 'game' with that too and it is important to keep it simple^^. Thanks for thinking along with me though! I really appreciate it.

ps. Is the only way to reward you to accept your answer? You have been really helpful, but it doesnt not work smoothly yet, so it might confuse other users if I accept it?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Transform Position X/Y increasing by no reason 2 Answers

Rotating an Object (Like Handle in Unity) 3 Answers

How to rotate an object according to the camera's view? 1 Answer

How to rotate gun only on X and Y axis? 1 Answer

Weird Results When Changing Transform And Rotation 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