Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Apr 18, 2013 at 05:07 AM · c#cameracharactercontroller

C# FP_Camera not following Character Controller

Hi everyone, I have been following the 3dbuzz tutorial series on how to make a 3rd person controller. I have this problem with the camera. I've noticed that the camera is not staying with the Character Controller when the character controller moves. I'm not sure what makes the camera stay with the character controller. I tried parenting the camera to the character controller but that didn't change anything. Any help would be appreciated.

TP_Controller

 using UnityEngine;
 using System.Collections;
 
 public class TP_Controller : MonoBehaviour {
     public static CharacterController characterController;
     public static TP_Controller instance;
     // Use this for initialization
     void Awake () {
     characterController = GetComponent("CharacterController") as CharacterController;
     instance = this;
     TP_Camera.UseExistingOrCreateNewMainCamera();
     }
     
     // Update is called once per frame
     void Update () {
      if(Camera.main == null)
             return;
         
     GetLocomotionInput();
         
     TP_Motor.instance.UpdateMotor();
     }
     void GetLocomotionInput(){
     var deadZone = 0.1f;
         
     TP_Motor.instance.moveVector = Vector3.zero;
       if(Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone )
       TP_Motor.instance.moveVector += new Vector3(0,0,Input.GetAxis("Vertical"));
         
       if(Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone )
       TP_Motor.instance.moveVector += new Vector3(Input.GetAxis("Horizontal"),0,0);
     }
 }


TP_Motor

 using UnityEngine;
 using System.Collections;
 
 public class TP_Motor : MonoBehaviour {
     
     public static TP_Motor instance;
     public float moveSpeed = 10f;
     public Vector3 moveVector {get; set;}
     // Use this for initialization
     void Awake () {
      instance = this;
     }
     
     // Update is called once per frame
     public void UpdateMotor () {
      SnapAlignCharacterWithCamera();
      ProcessMotion();
     }
     
     void ProcessMotion(){
     moveVector = transform.TransformDirection(moveVector);
     if(moveVector.magnitude > 1)
     moveVector = Vector3.Normalize(moveVector);
         
         moveVector *= moveSpeed;
         
         moveVector *= Time.deltaTime;
         
         TP_Controller.characterController.Move(moveVector);
     }
     
     void SnapAlignCharacterWithCamera(){
      if(moveVector.x != 0 || moveVector.x != 0){
      transform.rotation = Quaternion.Euler(transform.eulerAngles.x, Camera.mainCamera.transform.eulerAngles.y, transform.eulerAngles.z);
         }    
     }
 }

TP_Camera

 using UnityEngine;
 using System.Collections;
 
 public class TP_Camera : MonoBehaviour {
  
     public static TP_Camera instance;
     public Transform TargetLookAt;
     public float distance = 5f;
     public float distanceMin = 3f;
     public float distanceMax = 10f;
     public float distanceSmooth = 0.05f;
     public float x_MouseSensitivity = 5f;
     public float y_MouseSensitivity = 5f;
     public float mouseWheelSensitivity = 5f;
     public float x_Smooth = 0.05f;
     public float y_Smooth = 0.1f;
     public float y_minLimit = -40f;
     public float y_maxLimit = 80f;
     private float mouseX = 0f;
     private float mouseY = 0f;
     
     private float velX = 0f;
     private float velY = 0f;
     private float velZ = 0f;
     private float velDistance = 0f;
     private float startDistance = 0f;
     private Vector3 position = Vector3.zero;
     private Vector3 desiredPosition = Vector3.zero;
     private float desiredDistance = 0f;
     void Awake(){
     instance = this;
     }
     // Use this for initialization
     void Start () {
     distance = Mathf.Clamp(distance,distanceMin,distanceMax);
     startDistance = distance;
     Reset();
     }
     
     // Update is called once per frame
     void LateUpdate () {
      if(TargetLookAt == null)
      return;
         
         HandlePlayerInput();
         CalculateDesirePosition();
         UpdatePosition();
     }
     
     void HandlePlayerInput(){
     var deadZone = 0.01f;
      if(Input.GetMouseButton(1)){
     mouseX += Input.GetAxis("Mouse X") * x_MouseSensitivity;
     mouseY -= Input.GetAxis("Mouse X") * y_MouseSensitivity;
         }
         mouseY = Helper.ClampAngle(mouseY, y_minLimit, y_maxLimit);
         
         if(Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > -deadZone){
          desiredDistance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * mouseWheelSensitivity, distanceMin, distanceMax);
         }
     }
     
     void CalculateDesirePosition(){
     distance = Mathf.SmoothDamp(distance, desiredDistance, ref velDistance, distanceSmooth);
         
     desiredPosition = CalculatePosition(mouseY, mouseX, distance);
     }
     
     Vector3 CalculatePosition(float rotationX, float rotationY, float dist){
      Vector3 direction = new Vector3(0,0, -distance);
      Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
      return TargetLookAt.position + rotation * direction;
     }
     
     void UpdatePosition(){
     var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, x_Smooth);    
     var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, y_Smooth);
     var posZ = Mathf.SmoothDamp(position.z, desiredPosition.x, ref velZ, x_Smooth);
     position = new Vector3(posX,posY,posZ);
     transform.position = position;
     transform.LookAt(TargetLookAt);
     }
     
     public void Reset(){
     mouseX = 0;
     mouseY = 0;
     distance = startDistance;
     startDistance = distance;
     }
     public static void UseExistingOrCreateNewMainCamera(){
      GameObject tempCamera;
      GameObject targetLookAt;
      TP_Camera  myCamera;
      
      if(Camera.mainCamera != null){
       tempCamera = Camera.mainCamera.gameObject;
         }
         else{
          tempCamera = new GameObject("Main Camera");
          tempCamera.AddComponent("Camera");
          tempCamera.tag = "MainCamera";
         }
         tempCamera.AddComponent("TP_Camera");
         myCamera = tempCamera.GetComponent("TP_Camera") as TP_Camera;
         
         targetLookAt = GameObject.Find("targetLookAt") as GameObject;
         
         if(targetLookAt == null){
         targetLookAt = new GameObject("targetLookAt");
         targetLookAt.transform.position = Vector3.zero;
         }
         myCamera.TargetLookAt = targetLookAt.transform;
         
     }
 }
 

  
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Apr 18, 2013 at 05:22 AM 0
Share

Do you have a script controlling your Camera? Did you child the camera to the character or the other way around? It sounds like there may be something that is setting its position your camera that needs to know about your character. I'm sorry, but I'm not familiar enough with the 3dbuzz tutorials to know what they've instructed you to do.

avatar image Ochreous · Apr 18, 2013 at 05:29 AM 0
Share

I child the camera to the character controller. I have some scripts that I made from the tutorials.Would posting those help?

avatar image iwaldrop · Apr 18, 2013 at 05:31 AM 0
Share

Yep! Posting code always helps. :)

avatar image Ochreous · Apr 18, 2013 at 05:46 AM 0
Share

Ok I posted the three scripts I'm using.The first two TP_Controller and TP_$$anonymous$$otor are attached to the character Controller and the third TP_Camera is attached to the camera.

avatar image iwaldrop · Apr 19, 2013 at 01:14 AM 0
Share

Ok. The code looks good, so I have a couple questions.

  1. Do you have, or does the script create, a GameObject called "targetLookAt"?

  2. If you do, are you sure that the camera isn't following that and that nothings moving it?

  3. When you move the GameObject called targetLookAt, does the camera move?

If none of the above helps, then you should put a Debug.Log() statement in TP_Camera's LateUpdate after the line that says 'return'.

Let me know how it works out.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Decrease Mouse Sensitivity Camera Move 0 Answers

C# Twitchy Camera Movement 1 Answer

C# Need Help converting from Rigidbody to Character Controller 0 Answers

C# CharacterController.SimpleMove Goes on Forever 1 Answer

C# Character Controller Collision If Statement 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges