简体   繁体   中英

Unity3D C#. Using the transform.RotateAround() function while keep the distance between two objects constant

I am trying to practice making third person controllers in Unity 3D. I am a beginner, and I am completely baffled on how to make my controller functional. I have been doing hours of research, but no thread I can find seem to answer my question. I have two scripts, a CameraController, and a CharacterController. My code is below.

CameraController:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {
public GameObject target;
public float rotationSpeed;
Vector3 offset;
Vector3 CameraDestination;


// Use this for initialization
void Start () {
    offset = transform.position - target.transform.position;
    CameraDestination = offset + transform.position;
    rotationSpeed = 50f;
    transform.position = CameraDestination;
}

// Update is called once per frame
void Update () {

    transform.LookAt (target.transform.position);
    float h = Input.GetAxisRaw ("Horizontal");
    transform.RotateAround (target.transform.position, Vector3.up, Time.deltaTime * h * rotationSpeed);

    target.transform.Rotate (0f, Time.deltaTime * h * rotationSpeed, 0f);

}
}

CharacterController:

using UnityEngine;
using System.Collections;

public class CharController : MonoBehaviour {

public float playerSpeed = 10f;


// Use this for initialization
void Start () {



}

// Update is called once per frame
void Update () {
    float Vertical = Input.GetAxis("Vertical");
    transform.position += transform.forward * Time.deltaTime * playerSpeed * Vertical;



}
}

When either the left or right arrow key is pressed, both the player and the camera rotates. If I try to attach the camera to the player as a children, the camera's rotation becomes messed up, but if I do not attach the camera to the player, the camera stops following the player. If I try to set the camera to a specific position relative to the player, it stops revolving around the player like it is intended to do. I simply can not come up with a method that works.Thank you for answering my question in advance!

When I go about this, I like to have an empty gameObject which has 2 children, the camera and then the mesh of the character.

> CharacterController
    > Camera
    > CharacterRig

When you want to rotate the character, rotate the CharacterController, then when you want to rotate the Camera around the character, change your code to:

transform.RotateAround (CharacterRig.transform.position, Vector3.up, Time.deltaTime * h * rotationSpeed);

This will allow your camera to rotate irrespective of any character animation and should solve your issue. This is very important if you want to implement animations later, as you don't want to parent the camera to the thing that is being animated because it will move with the animation.

Ps Your code appears fine. Certain that it is purely the way you have set up the game objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM