Restricting rotation around only two axes

I’m using the faux gravity codes floating around to walk around the inside of a cylinder.
The problem with this is that my player is rotating around a point whereas I’d like the player to rotate along the axis of the cylinder. I have stopped the force that pushes the player down the cylinder but I can’t stop the player from rotating around the centre point.

the code for FauxGravityAttractor I’m using is below.

`using UnityEngine;

using System.Collections;

public class FauxGravityAttractor : MonoBehaviour {

public float gravity = -12;

public void Attract(Transform body) {
	Vector3 gravityUp = (body.position - transform.position).normalized;
	Vector3 localUp = -body.up;
	float Xcomponent = (gravityUp * gravity).x;
	float Ycomponent = (gravityUp * gravity).y;


	Quaternion targetRotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
	body.rotation = Quaternion.Slerp(body.rotation, targetRotation, 50f * Time.deltaTime);



I’ve been slowly learning this for a week or two now so any simple help will be greatly appreciated!

  1. Select the item you want to restrict rotation on.
  2. In the inspector, goto Rigidbody > Constraints
  3. Here you can set the axes who are able to receive rotation

If this is not enough, I can only code in JS so can’t help you further.

looking into your code it seems, you want to restrict rotation along z axis .

In the inspector, goto Rigidbody > Constraints >Freeze Rotation > check z axis