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 digitalni · Sep 18, 2012 at 12:14 AM · cameramousesmoothshootertop down

Top down shooter camera follow cursor

Hi, I'm facing a problem that I can't seem to solve. I want the camera to follow the mouse cursor, but the player character to remain visible. So the camera can't go a certain distance away from the player. Can anybody clear some stuff up for me?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by simco500 · Jan 20, 2013 at 02:53 AM

I think I know what you mean, I've had the same problem but with a little thinking I got it. I made a script for the camera where it calculates the middle between the Player and the position of the cursor. Then I only needed to make something that made the camera follow that position in the x and z direction and voila :). You can tweak it as much if you want. This script I made was perfect for my project, I hope it'll work with you too.

 var Damping = 5.0;
 var Player : Transform;
 var Height : float = 4;
 var Offset : float = 5;
 
 private var Center : Vector3;
 var ViewDistance : float = 5.0;
 
 function Update () 
 {
     var mousePos = Input.mousePosition;    
     mousePos.z = ViewDistance; 
     var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos); /
     
     var PlayerPosition = Player.position; 
     
     Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2); 
     
     transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping); 
 }
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
1
Wiki

Answer by simco500 · Jan 20, 2013 at 02:53 AM

Hey, I've tought a little about your problem and here's what I did: calculate the middle between the player and the position of the mouse in the world. Let the camera smooth follow that middle. Actually quite simple but it took me quite a while to come up with.

 var Damping = 12.0;
 var Player : Transform;
 var Height : float = 13.0;
 var Offset : float = 0.0;
 
 private var Center : Vector3;
 var ViewDistance : float = 3.0;
 
 function Update () 
 {
     var mousePos = Input.mousePosition;
     mousePos.z = ViewDistance;
     var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
     
     var PlayerPosition = Player.position;
     
     Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
     
     transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping); 
 }
Comment
Add comment · Show 4 · 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 Jaxobz · Dec 13, 2014 at 10:18 AM 0
Share

Thank you friend this worked brilliantly!

avatar image FAL0 · Apr 06, 2020 at 07:56 PM 0
Share

I've never seen a copy paste work so easly. I love you

(from 2020)

avatar image jjohn8521 FAL0 · Mar 22, 2021 at 02:13 PM 0
Share

How is this code working for you? I'm getting about 26 errors, like

Assets\Player\Scripts\CameraFollow.cs(5,42): error CS1003: Syntax error, ',' expected

Assets\Player\Scripts\CameraFollow.cs(13,19): error CS1002: ; expected

Assets\Player\Scripts\CameraFollow.cs(13,19): error CS1022: Type or namespace definition, or end-of-file expected

Assets\Player\Scripts\CameraFollow.cs(13,19): error CS1022: Type or namespace definition, or end-of-file expected

Assets\Player\Scripts\CameraFollow.cs(13,21): error CS0116: A namespace cannot directly contain members such as fields or methods

Assets\Player\Scripts\CameraFollow.cs(13,27): error CS1022: Type or namespace definition, or end-of-file expected

Assets\Player\Scripts\CameraFollow.cs(19,25): error CS1002: ; expected

Assets\Player\Scripts\CameraFollow.cs(19,25): error CS1513: } expected

avatar image jjohn8521 FAL0 · Mar 22, 2021 at 02:15 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour
  
  var Damping = 12.0;
  var Player : Transform;
  var Height : float = 13.0;
  var Offset : float = 0.0;
  
  private var Center : Vector3;
  var ViewDistance : float = 3.0;
  
  function Update () 
  {
      var mousePos = Input.mousePosition;
      mousePos.z = ViewDistance;
      var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
      
      var PlayerPosition = Player.position;
      
      Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
      
      transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping); 
  }

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

15 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

Related Questions

Smooth Camera Movement script with problem. 0 Answers

2d camera help! 0 Answers

3D camera smooth rotation and mouse wheel zoom 2 Answers

How do I rotate a character with my mouse? 1 Answer

Cannonball not shooting according to the rotation of the cannon. 1 Answer


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