How to rotate an object around a player when pushing a 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 use them individually. But If I combine them The blueCube does not fully rotate around the redCube, but makes circles beside it (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);
}
}
Your answer
Follow this Question
Related Questions
How I can i rotate a bullet after spawning relative to the mouseposition? 0 Answers
Axis are not rotated as I would expect. 0 Answers
How to find current vertical axis 1 Answer
Make GameObject rotate around another around random axis but with fixed distance 0 Answers
Rotate object with mouse 0 Answers