using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOrbit : MonoBehaviour
{
public GameObject asteroid;
public GameObject player;
public GameObject rayFollow;
public GameObject offset;
public GameObject offsetFollow;
public GameObject parent;
public Camera cam;
RaycastHit hit;
RaycastHit followHit;
private void FixedUpdate()
{
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
LayerMask layerMask = ~((1 << 7) | (1 << 8) | (1 << 9));
Physics.Raycast(ray, out hit, 50, layerMask);
Debug.DrawLine(ray.origin, hit.point, Color.green);
rayFollow.transform.position = hit.point;
offsetFollow.transform.LookAt(transform);
if (Vector3.Distance(offset.transform.position, offsetFollow.transform.position) > 1f)
{
offsetFollow.transform.position = Vector3.Lerp(offsetFollow.transform.position, offset.transform.position, 2f * Time.deltaTime);
}
Vector3 fwd = offsetFollow.transform.TransformDirection(Vector3.forward);
Debug.DrawRay(offsetFollow.transform.position, fwd * 50, Color.blue);
Physics.Raycast(offsetFollow.transform.position, fwd, out followHit, 50, layerMask);
// Parent object is set to the raycast hitpoint
parent.transform.position = followHit.point;
parent.transform.rotation = Quaternion.Lerp(parent.transform.rotation, Quaternion.FromToRotation(Vector3.up, followHit.normal), 3f * Time.deltaTime);
// Player follows the parent but has a separate look rotation towards the movement direction
player.transform.LookAt(rayFollow.transform);
player.transform.localEulerAngles = new Vector3(0, player.transform.localEulerAngles.y, 0);
// Here I rotate the camera
if (Input.GetKey(KeyCode.W))
{
transform.Rotate(Vector3.right * 40f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Rotate(Vector3.left * 40f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up * 40f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.down * 40f * Time.deltaTime);
}
}
}
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
Here’s how to do it:
You may be able to simply drive transform.forward (or transform.up when in 2D) equal to your movement vector.
Otherwise, use Mathf.Atan2() to derive heading:
Sine/Cosine/Atan2 (sin/cos/atan2) and rotational familiarity and conventions
I solved the problem by myself but I can’t figure out how to delete this thread
You are not supposed to delete that. The entire #%#^#$ point of writing posts in the forum is for everybody to benefit, not just for you to skulk in, get your information, skulk back out and destroy everything in your wake.
Don’t do that, don’t be so selfish. Your post has been reported in order to get it restored properly.
Sorry, still new to posting on this forum. I put the code back and presented how I solved the problem.