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 DnC_Gamez · Mar 31, 2014 at 05:15 PM · camerarotatebeginner

Help getting the target and camera rotation to match

ok so i am like making a game camera like the one from wizard 101 and i got everything else done that i need but when i rotate the target(aka player) in place the camera does not rotate with it facing the same direction as the target(aka player) i have tried many tutorials but cant seem to get any to work with the way my setup is so if anyone can help that would be great below is my code for the camera follow(java script) and player movement(java script) please help me fix my problem been working on it for 3 days now and to no luck with it...and i have also tried making the target the cameras parent.

camera code:

 #pragma strict
 // setup variables
 var layermask: LayerMask;
 var target : Transform = null;;
 var height : float = 0.5;
 var distance : float = 4;
 
 var x:float = 0;
 var y:float = 4;
 var ymin:float = -50;
 var ymax:float = 60;
 var disMin:float = 1;
 var disMax:float = 8;
 var TurnSpeed : float = 4;
 
 // update the camera every screen refresh
 function Update () {
     // supose to lock on to target
     transform.LookAt(target.transform);
     // check if right mouse button is being pressed
     if(Input.GetMouseButton(1))
     {
         //updates camera's direction by mouse x and mouse y
         x += 10 * Input.GetAxis("Mouse X");
         y += 10 * Input.GetAxis("Mouse Y");
         // makes sure camera doesn't go pass its set y cords
         if(y > ymax)
         {
             y = ymax;
         }else if(y < ymin){
             y = ymin;
         }
     }
     // checks if mouse scroll is scrolling forward
     if(Input.GetAxis("Mouse ScrollWheel") > 0 && distance > disMin)
     {
         // zooms in foward and updates the height
         distance -= 0.2;
         if(height > 0.5)
         {
             height -= 0.05;
         }
         else if(height < 0.5)
         {
             height = 0.5;
         }
     }
     //checks if mouse scroll is scrolling backwards
     else if(Input.GetAxis("Mouse ScrollWheel") < 0 && distance < disMax)
     {
         // scrolls camera out and up at a slight tilt
         distance += 0.2;
         height += 0.05;
     }
     // check if scroll button is pressed
     if(Input.GetMouseButton(2))
     {
         // reesets camera to fixed point
         y = 4;
         height = 0.5;
         distance = 4;
     }
     
     // this block of code allows for the cam being pushed forward when againts terrain type stuff
     //{
     var rotation = Quaternion.Euler(y,x,0);
     var position = rotation * Vector3(0.0,height,-distance) + target.position;
     
     
     transform.rotation = rotation;
     transform.position = position;
     
     // check for collisions on layermask
     var hit : RaycastHit;
     if(Physics.Linecast(target.position,transform.position,hit,layermask))
     {
         var tempDistance = Vector3.Distance(target.position, hit.point);
         position = rotation * Vector3(0.0,height,-tempDistance) + target.position;
         transform.position = position;
     }
     //}
 }
 


target(aka player) movement:

 #pragma strict
 // setup variables
  var MoveSpeed : float = 0.2;
  var TurnSpeed : float = 4;
  
 function Update(){
     // moves this fowards
     if(Input.GetKey("w") || Input.GetKey(KeyCode.UpArrow))
     {
         this.transform.position += this.transform.forward * this.MoveSpeed;
     }
     // moves this backwards 
     if(Input.GetKey("s") || Input.GetKey(KeyCode.DownArrow))
     {
         this.transform.position -= this.transform.forward * this.MoveSpeed;
     }
     // rotates in place to the left
     if(Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
     {
         this.transform.Rotate(0,-this.TurnSpeed,0);
     }
     // rotates in place to the right
     if(Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
     {
         this.transform.Rotate(0,this.TurnSpeed,0);
     }
     // updates this to face camera's direction when right mouse button is down
     if(Input.GetMouseButton(1)) {
         this.transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
     }
 }

please help me and thanks in advanced for any and all help

Comment
Add comment · Show 4
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 LMan · Mar 31, 2014 at 06:42 PM 0
Share

So the camera doesn't move at all? Or does it respond to those controls in the camera follow script- mouse buttons/scroll and such?

avatar image DnC_Gamez · Apr 10, 2014 at 07:32 PM 0
Share

the camera works it just when i rotate in place with my character the camera doesn't stay behind it and doesn't face it in the same direction

avatar image robertbu · Apr 10, 2014 at 08:27 PM 0
Share

I think you need a better description of correct behavior. Right now you are using the mouse to rotate the camera, so adding an additional criteria of matching the rotation of the target will cause fighting. Currently your code looks at the character on line 19, but then you overwrite this rotation with one on line 66 that uses "$$anonymous$$ouse X" and "$$anonymous$$ouse Y". It also doesn't appear that you are causing the camera to follow the player, so matching the rotation doesn't make sense to me. $$anonymous$$aybe you need to start with a copy of the standard CameraFollow script and then put in your additions.

avatar image DnC_Gamez · Apr 10, 2014 at 08:47 PM 0
Share

ok ty i will try that

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Help with understanding and modifying a camera script (JavaScript) 1 Answer

Why is my camera switching angles? 0 Answers

Spawned an object but it faces the wrong way. What's the fix? 2 Answers

Fps Controller movement is broken 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