I’m making a 3D platformer game in which you control a cube using mouse clicks. The concept is, you click in the opposite direction of where you want the cube to go (i.e if you want it to jump up you click under it). I would like to include an object that points in the direction the cube so that the player has some idea of where the cube will go when they click. The problem is that I would like the pointer to always orbit around the player in relation to the mouse pointer position at a fixed distance.
This is what I have so far: Bandicam 2018-02-12 10-02-41-830 GIF | Gfycat
Here’s the code:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float fallMulti = 2.5f;
public float lowJumpMulti = 2f;
public GameObject pointer; //The pointing capsule
// public int jumpLimit = 3;
public float thrust = 10;
Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody> ();
}
void Update() {
Vector3 mousePos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z);
mousePos = Camera.main.ScreenToWorldPoint (mousePos); // Get world pos relative to mouse pos
Vector3 targetDirection = mousePos - transform.position;
Quaternion newRotation = Quaternion.LookRotation (Vector3.forward, targetDirection);
Vector3 newDir = newRotation * Vector3.up;
if (Input.GetMouseButtonDown (0)) {
// transform.rotation = newRotation;
// GetComponent<Rigidbody> ().AddForce (newDir * thrust, ForceMode.VelocityChange);
rb.velocity = newDir * thrust;
}
pointer.transform.position = new Vector3 (mousePos.x, mousePos.y, transform.position.z);
pointer.transform.rotation = newRotation;
if (rb.velocity.y < 0) {
rb.velocity += Vector3.up * Physics.gravity.y * (fallMulti - 1) * Time.deltaTime;
} else if (rb.velocity.y > 0 && !Input.GetMouseButton(0)) {
rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMulti - 1) * Time.deltaTime;
}
// else if (rb.velocity.y > 0){
// rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMulti - 1) * Time.deltaTime;
// }
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "Platform") {
}
}
}
How could I get the the capsule to rotate around the player at a fixed distance?