- Home /
how to rotate a Rigidbody2D player on a moving platform.
Hello i managed to create a script that makes my player move on a platform in top down perspective. I also want that when the platform is rotating the player rotates too, but not around its own middlepoint.
this is how it looks now:
as you can see the platform is rotating but the player is not rotating with it.and i the second gif you can see that the platform is constantly moving right and the player is moving with it. The code that i use to achieve this is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Rigidbody2D platform;
public Camera cam;
Vector2 movement;
Vector2 mousePos;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
Vector2 pos;
private void FixedUpdate()
{
pos = pos + movement;
rb.MovePosition(platform.position + pos * moveSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y,lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
so this is what i want to achieve. (link to imgur because im only allowed to post 2 attachments) picture
how do i make it so that the player will rotate along side the platform while still being able to move ?
Your answer
Follow this Question
Related Questions
Rigidbody rotation: Use hinge joint motor or MoveRotation script? 0 Answers
making a mushroom trampoline , bouncing the gameobject relative to mushroom's rotation 1 Answer
Do something every few degrees of a rotation? 1 Answer
How Do I Make My Weapon Follow My Cursor? 0 Answers
How Do I rotate a Object Based On the player's location? 1 Answer