- Home /
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);
}
}
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);
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^^.
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;
}
}
}
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?