Top-down aim in sphere
Hello everyone.
I would like to know how could I make exactly the same aim system for the player with spherical movement using the mouse position like this game: https://www.youtube.com/watch?v=aqDagg11MOE
The fake gravity I used is from Sebastian Lague . It really works, but the problem here is that I need the camera look to the player and stay 15 meters in Y axis of the local position of the player. Don't know if it has to be a child of the player, or have a script that follow the player and change his rotation / position towards the player. Link: https://www.youtube.com/watch?v=TicipSVT-T8
Playing with the code, I only managed to make the model go crazy when using raycast to locate the mouse in the world. Another option I tried was to use Graphic Raycaster. I don't really understand this last one so I couldn't do much...
It would really help if someone could show me how I could update the code so that the player turns to the position of the mouse (target), and even then its rotation / position due to the false gravity of the planet is correct.
Thanks for reading and helping.
Fake gravity
public class FakeGravity : MonoBehaviour {
public float gravity = -50;
public void Attract(Transform objBody){
// Set planet gravity direction for the object body
Vector3 gravityDir = (objBody.position - transform.position).normalized;
Vector3 bodyUp = objBody.up;
// Apply gravity to objects rigidbody
objBody.GetComponent<Rigidbody>().AddForce(gravityDir * gravity);
// Update the objects rotation in relation to the planet
Quaternion targetRotation = Quaternion.FromToRotation(bodyUp, gravityDir) * objBody.rotation;
// Smooth rotation
objBody.rotation = Quaternion.Slerp(objBody.rotation, targetRotation, 50 * Time.fixedDeltaTime);
}
}
Physics of the objects
public class FakeGravityBody : MonoBehaviour {
public FakeGravity attractor;
Rigidbody rb;
void Awake(){
rb = GetComponent<Rigidbody>();
}
void Start () {
rb.constraints = RigidbodyConstraints.FreezeRotation;
rb.useGravity = false;
}
void FixedUpdate () {
attractor.Attract(transform);
}
}
Player controller in sphere
public class PlayerMovementScript : MonoBehaviour {
public float walkSpeed = 6;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
Rigidbody rb;
void Awake(){
rb = GetComponent<Rigidbody>();
}
void Update(){
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX, 0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, .15f);
}
void FixedUpdate(){
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rb.MovePosition(rb.position + localMove);
}
}
1. Edit: I figured out how could implement the aim system. A) The camera has to be a child of the player. I should rotate the model of the player (not the player itself) towards the mouse, but at the same time, the rotation should be in sync with the player rotation (don't know how could I do that) B) The camera has a script that follow the player's position and rotation (tamera should not rotate through rotation when aiming), and the rotation is integrated in the same script of the player movement.
Your answer
![](https://koobas.hobune.stream/wayback/20220612205522im_/https://answers.unity.com/themes/thub/images/avi.jpg)