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 /
This question was closed Nov 03, 2016 at 12:53 PM by Landern for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by boomcrash · Sep 21, 2010 at 02:23 AM · cameratopdowndiabloscript-ready-to-use

Diablo Style Camera and Movement

How do I setup a camera that functions like the camera in Diablo or Torchlight, one that moves with the player, but does not rotate?

Also, the player needs to move like the characters in Diablo.

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

4 Replies

  • Sort: 
avatar image
6
Best Answer

Answer by LucasGaspar · Sep 21, 2010 at 04:16 AM

 using UnityEngine;
 using System.Collections;
 public class CameraController : MonoBehaviour 
 {
     public GameObject player;  //The offset of the camera to centrate the player in the X axis
     public float offsetX = -5;  //The offset of the camera to centrate the player in the Z axis
     public float offsetZ = 0;  //The maximum distance permited to the camera to be far from the player, its used to     make a smooth movement
     public float maximumDistance = 2;  //The velocity of your player, used to determine que speed of the camera
     public float playerVelocity = 10;
 
     private float movementX;
     private float movementZ;
 
     // Update is called once per frame
     void Update () 
     {
         movementX = ((player.transform.position.x + offsetX - this.transform.position.x))/maximumDistance;
         movementZ = ((player.transform.position.z + offsetZ - this.transform.position.z))/maximumDistance;
         this.transform.position += new Vector3((movementX * playerVelocity * Time.deltaTime), 0, (movementZ * playerVelocity * Time.deltaTime));
     }
 }

Put this script in a camera in your game, adjust the variables to your needs, you have to drop your Player to the variable "player". Hope this helps!

Comment
Add comment · Show 6 · 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 boomcrash · Sep 22, 2010 at 02:26 AM 0
Share

I got the following error when I pasted the code in a in a blank C# script: The importer for asset Assets/$$anonymous$$odels/Scripts/NewBehaviourScript.cs ($$anonymous$$onoImporter) did not generate a $$anonymous$$D4 digest.

avatar image LucasGaspar · Sep 22, 2010 at 06:29 AM 0
Share

Oh, you have to name the script "CameraController".

avatar image boomcrash · Sep 23, 2010 at 01:37 AM 0
Share

Sweet!! thanks that didnt!!!!!

avatar image LucasGaspar · Oct 05, 2010 at 01:25 AM 0
Share

Vote for my post ^^

avatar image Shantred · Apr 24, 2012 at 12:36 AM 0
Share

I know this is old, but I have a question about this. When thrown onto my third person controller, when I hit play, the camera focuses somewhere on the ground where I can't see the player. I dragged my "3rd person controller" to the player variable. Is this correct usage?

Show more comments
avatar image
4

Answer by fscopel · Sep 17, 2016 at 12:51 AM

This is the exactly the same code as above just nicely formatted. Thank you @LucasGasper

 using UnityEngine;
 using System.Collections;
 
 public class CameraControlScript : MonoBehaviour
 {
     public GameObject player;
     
     //The offset of the camera to centrate the player in the X axis 
     public float offsetX = -5;
     //The offset of the camera to centrate the player in the Z axis 
     public float offsetZ = 0;
     //The maximum distance permited to the camera to be far from the player, its used to make a smooth movement 
     public float maximumDistance = 2;
     //The velocity of your player, used to determine que speed of the camera 
     public float playerVelocity = 10;
 
     private float movementX;
     private float movementZ;
 
     // Update is called once per frame 
     void Update()
     {
         movementX = ((player.transform.position.x + offsetX - this.transform.position.x))/maximumDistance;
         movementZ = ((player.transform.position.z + offsetZ - this.transform.position.z))/maximumDistance;
         this.transform.position += new Vector3((movementX * playerVelocity * Time.deltaTime),0, (movementZ * playerVelocity * Time.deltaTime));
     }
 }
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
2

Answer by Manfice · Nov 03, 2016 at 12:53 PM

I suggest to add Y axis, If you wont look like izometric set camera rotation manualy x=30, y=40, z=0. Y axis will correct a height of camera.

   public class IzoCamera : MonoBehaviour
      {
     public GameObject Player;
     public float OffsetX = -35;
     public float OffsetZ = -40;
     public float OffsetY = 30;
     public float MaximumDistance = 2;
     public float PlayerVelocity = 10;

     private float _movmentX;
     private float _movmentY;
     private float _movmentZ;

     // Use this for initialization
     void Start ()
     {
         
     }
 
     // Update is called once per frame
     void Update ()
     {
         _movmentX = ((Player.transform.position.x + OffsetX - this.transform.position.x)) / MaximumDistance;
         _movmentY = ((Player.transform.position.y + OffsetY - this.transform.position.y)) / MaximumDistance;
         _movmentZ = ((Player.transform.position.z + OffsetZ - this.transform.position.z)) / MaximumDistance;
         this.transform.position += new Vector3((_movmentX * PlayerVelocity * Time.deltaTime), (_movmentY * PlayerVelocity * Time.deltaTime), (_movmentZ * PlayerVelocity * Time.deltaTime));
     }
 }
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 Graham · Sep 21, 2010 at 03:36 AM

If you're not interested in rotation or anything fancy, you can just "lock" the camera on your player model by making the camera a child of the character - just drag the camera in the hierarchy on top of your character in the hierarchy. Then, in the editor, move your view (the actual editor view, not the camera object) so it looks at the character exactly the way you want the camera to look at your character. Then select the camera in the hierarchy and choose "align with view" from the "GameObject" menu at the top. That will position the camera at that view. When you run the game, the camera should move along with your player.

Comment
Add comment · Show 2 · 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 boomcrash · Sep 22, 2010 at 03:06 AM 0
Share

I would actually only like the camera to translate with the character, but not rotate. So the cameras rotation is locked to the world.

avatar image RickHurd · Jan 29, 2014 at 06:31 PM 0
Share

is there a JavaScript equivalent for this script? I tried translating it but I'm a noob :P

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotate From SpawnPoint TO Transform Rotation 1 Answer

Camera looking down 1 Answer

Move camera and player OR move objects? 1 Answer

Top down camera that follows the player and rotates with the player while remaining constrained to world bounds 0 Answers

3rd Person Camera: Keeping it out of objects etc (example)? 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