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 unity1 · Apr 22, 2014 at 06:40 AM · camera movementstealth

Stealth Project Tutorial Camera Movement

I have copy-pasted the code but still camera movement is not working as expected. It just rotates smoothly from the initial scene start towards initial position of the character and just stays looking from top-bottom and without following Ethan.

I am not getting any warnings or errors. What I might have missed? I think there is nothing much to be done to the main_camera game object apart from attaching the CameraMovement code attached to it. Please help

Update:

I have started from scratch again and re done the entire exercise again and got the same problem. This time I tried to debug the code with debug statements and I found interesting thing. What I did is that tried to trace the player position and camera position and what I have noticed is that player position doesn't change at all

 void FixedUpdate ()
     {
         // The standard position of the camera is the relative position of the camera from the player.
         Vector3 standardPos = player.position + relCameraPos;
         
         // The abovePos is directly above the player at the same distance as the standard position.
         Vector3 abovePos = player.position + Vector3.up * relCameraPosMag;
 
         Debug.Log ("Camera Movement:" + standardPos.ToString() + " -- " + abovePos.ToString());
 

Output of debug statment

Camera Movement:(-5.2, 8.1, -4.7) -- (0.0, 10.2, 0.0)

I was expecting the standardPos which is the player's position (standardPos) will be -2.5,0,0 (which is initial position of ethan) and abovePos will be relative to the initial position of ethean. To my surprise standard position is showing (0,10.2,0) and the values doesnt change even when I move the player (though it moves in the game)

Also, when I traced player position in the Awake() function it shows (0,0,0) instead of expected (-2.5,0,0).

What might the problem? the code referencing the player variable to something different object instead of ethen? How do I find it?

Below is the copy - pasted code (without debug statements)

 using UnityEngine;
 using System.Collections;
 
 public class CameraMovement : MonoBehaviour
 {
     public float smooth = 1.5f;         // The relative speed at which the camera will catch up.
     
     
     private Transform player;           // Reference to the player's transform.
     private Vector3 relCameraPos;       // The relative position of the camera from the player.
     private float relCameraPosMag;      // The distance of the camera from the player.
     private Vector3 newPos;             // The position the camera is trying to reach.
 
     
     void Awake ()
     {
         // Setting up the reference.
         player = GameObject.FindGameObjectWithTag(Tags.player).transform;
         
         // Setting the relative position as the initial relative position of the camera in the scene.
         relCameraPos = transform.position - player.position;
         relCameraPosMag = relCameraPos.magnitude - 0.5f;
     }
     
     
     void FixedUpdate ()
     {
         // The standard position of the camera is the relative position of the camera from the player.
         Vector3 standardPos = player.position + relCameraPos;
         
         // The abovePos is directly above the player at the same distance as the standard position.
         Vector3 abovePos = player.position + Vector3.up * relCameraPosMag;
         
         // An array of 5 points to check if the camera can see the player.
         Vector3[] checkPoints = new Vector3[5];
         
         // The first is the standard position of the camera.
         checkPoints[0] = standardPos;
         
         // The next three are 25%, 50% and 75% of the distance between the standard position and abovePos.
         checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
         checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
         checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
         
         // The last is the abovePos.
         checkPoints[4] = abovePos;
         
         // Run through the check points...
         for(int i = 0; i < checkPoints.Length; i++)
         {
             // ... if the camera can see the player...
             if(ViewingPosCheck(checkPoints[i]))
                 // ... break from the loop.
                 break;
         }
         
         // Lerp the camera's position between it's current position and it's new position.
         transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
         
         // Make sure the camera is looking at the player.
         SmoothLookAt();
     }
     
     
     bool ViewingPosCheck (Vector3 checkPos)
     {
         RaycastHit hit;
         
         // If a raycast from the check position to the player hits something...
         if(Physics.Raycast(checkPos, player.position - checkPos, out hit, relCameraPosMag))
             // ... if it is not the player...
             if(hit.transform != player)
                 // This position isn't appropriate.
                 return false;
         
         // If we haven't hit anything or we've hit the player, this is an appropriate position.
         newPos = checkPos;
         return true;
     }
     
     
     void SmoothLookAt ()
     {
         // Create a vector from the camera towards the player.
         Vector3 relPlayerPosition = player.position - transform.position;
         
         // Create a rotation based on the relative position of the player being the forward vector.
         Quaternion lookAtRotation = Quaternion.LookRotation(relPlayerPosition, Vector3.up);
         
         // Lerp the camera's rotation between it's current rotation and the rotation that looks at the player.
         transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
     }
 }
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 Ed unity · Apr 22, 2014 at 05:03 PM 0
Share

Without the code that your script is running, it is hard to say what the actual issue is. It could be that the script is not attached to the camera, the camera could be a child of another object which is overriding your script. Please provide the code for your camera movement.

avatar image unity1 · Apr 23, 2014 at 04:25 AM 0
Share

I did't posted the code because its just copy & pasted. Any way I can still post it

avatar image getyour411 · Apr 23, 2014 at 04:47 AM 0
Share

Check Ethan's tag is player

avatar image unity1 · Apr 23, 2014 at 04:51 AM 0
Share

@getyour411 yes its tagged as player already

avatar image unity1 · Apr 24, 2014 at 04:43 PM 0
Share

@ed unity and @getyour411 I have updated the OP. Please help

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Turlogh · Apr 28, 2015 at 01:14 PM

I had the same problem. I found, that there were more objects with tag Player. The bad ones were "char_ethan (Clone)" and their Layer was 31 ... the correct was only "char_ethan" in layer 0. Problem was solved by restarting Unity.

Comment
Add comment · Share
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
0

Answer by abhi_360 · Jul 30, 2014 at 11:59 AM

restart unity that did the trick for me.seems to me like unity goes wack sometimes. the attached scripts with references to and dependent on position vectors just stop working especially the vectors it is more prominent if u r using a a DX10 GPU on a DX11 OS

Atleast i think this is the problem and solution

Comment
Add comment · Share
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
0

Answer by sl1m · Oct 10, 2015 at 04:00 PM

had similar problem. Restarting Unity just helped.

Comment
Add comment · Share
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

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

24 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 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

Stealth project enemy not shooting 0 Answers

Raycast radius 1 Answer

Enemy Line of Sight Help Needed 2 Answers

I am having a problem with trying to get the camera to follow the main character. 3 Answers

Raycast only works in cartain areas of scene 0 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