- Home /
Rotate player to aim on one axis (Z-axis) towards mouse position/joystick - 2.5D (3D)
I am fairly new to Unity and I am trying to make a game similar to Super Smash Bros. So a 2.5D type of game where plays only move horizontally. I want my players to be spheres with a smaller sphere that rotates 360 degrees around them to aim. Here is an example of what I mean:
Not the best angle, but what I am trying to do is have the green sphere rotate 360 degrees around the other sphere on one axis. I am trying to figure out the best way to do this/ a script to do it. I sort of had a script that would rotate the sphere towards the mouse, but it was glitchy and made the player movement not work well. Then I thought it may be easier to rig up a bone in blender from the center of the sphere out to the smaller "hand" sphere so I could just rotate the bone. I am still having trouble figuring out how to do this/ what the best way to do it is. Like I mentioned above, I want this resemble super smash bros, so there will be multiple people playing at a time so I also want to handle rotating the "hand" with a joystick as well. Any recommendations would be greatly appreciated!
Here is my movement script right now without any rotation code:
`
public class PlayerMovement : MonoBehaviour
{
public float speed = 10.0F;
public float jumpSpeed = 13.0F;
public float gravity = 25.0F;
public float amount = 10;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
private int jumpCount;
// Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1"))
{
rb.AddForce(Vector3.back * amount);
print("clicked");
}
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
jumpCount = 2;
if (Input.GetButtonDown("Jump"))
{
jumpCount -= 1;
moveDirection.y = jumpSpeed;
}
}
else
{
if (Input.GetButtonDown("Jump") && jumpCount > 0)
{
jumpCount -= 1;
moveDirection.y = jumpSpeed;
}
moveDirection.x = Input.GetAxis("Horizontal") * speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612161856im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Point an arrow at the mouse position, 2.5D 1 Answer
How to get 360 directions from [joystick/mouse] instead of 8? 1 Answer
No movement to negative z-axis. 1 Answer
advanced problem for me regarding transfering rotation from the gizmo to the object 0 Answers
Problem Rotating up vector of camera to mouse position on screen. 2 Answers