Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 flopshotQQ · Jul 19, 2015 at 05:47 PM · camera2dplatformaimcrosshair

2D Platform Shooter Camera Controls C#

So I haven't had much luck finding a solution for this.

I have two parts to my player control - the player object, and a cross-hair. I actually have a lot more but these are the two parents. I originally wasn't going to have an aim feature for the player, so I created a focus area for the camera around my player object. When the player reaches the edge of this box it pushes the camera in that direction. It has some more robust features that can be removed if needed.

I want to adjust this script (or write a new one) that allows the cross-hair to push the focus box as well, but only so long as the player is contained in it. The cross-hair will NOT be able to move the player, but the player WILL be able to move the cross-hair to keep it on screen.

Here's what I have so far

The CameraFollow Script (sorry you have to scroll side-ways to see it all):

public class CameraFollow : MonoBehaviour {

 public Controller2D target;
 public float verticalOffset;
 public float lookAheadDstX;

 public float lookSmoothTimeY;
 public float lookAheadDstY;

 public float lookSmoothTimeX;
 public float verticalSmoothTime;
 public Vector2 focusAreaSize;
 
 FocusArea focusArea;
 
 float currentLookAheadX;
 float targetLookAheadX;
 float lookAheadDirX;
 float smoothLookVelocityX;
 float smoothVelocityY;
 
 bool lookAheadStopped;
 
 void Start() {
     focusArea = new FocusArea (target.collider.bounds, focusAreaSize);
 }
 
 void LateUpdate() {
     focusArea.Update (target.collider.bounds);
     
     Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;
     
     if (focusArea.velocity.x != 0) {
         lookAheadDirX = Mathf.Sign (focusArea.velocity.x);
         if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0) {
             lookAheadStopped = false;
             targetLookAheadX = lookAheadDirX * lookAheadDstX;
         }
         else {
             if (!lookAheadStopped) {
                 lookAheadStopped = true;
                 targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX)/4f;
             }
         }
     }
     
     
     currentLookAheadX = Mathf.SmoothDamp (currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);
     
     focusPosition.y = Mathf.SmoothDamp (transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
     focusPosition += Vector2.right * currentLookAheadX;
     transform.position = (Vector3)focusPosition + Vector3.forward * -10;
 }
 
 void OnDrawGizmos() {
     Gizmos.color = new Color (1, 0, 0, .5f);
     Gizmos.DrawCube (focusArea.centre, focusAreaSize);
 }
 
 struct FocusArea {
     public Vector2 centre;
     public Vector2 velocity;
     float left,right;
     float top,bottom;
     
     
     public FocusArea(Bounds targetBounds, Vector2 size) {
         left = targetBounds.center.x - size.x/2;
         right = targetBounds.center.x + size.x/2;
         bottom = targetBounds.min.y;
         top = targetBounds.min.y + size.y;
         
         velocity = Vector2.zero;
         centre = new Vector2((left+right)/2,(top +bottom)/2);
     }
     
     public void Update(Bounds targetBounds) {
         float shiftX = 0;
         if (targetBounds.min.x < left) {
             shiftX = targetBounds.min.x - left;
         } else if (targetBounds.max.x > right) {
             shiftX = targetBounds.max.x - right;
         }
         left += shiftX;
         right += shiftX;
         
         float shiftY = 0;
         if (targetBounds.min.y < bottom) {
             shiftY = targetBounds.min.y - bottom;
         } else if (targetBounds.max.y > top) {
             shiftY = targetBounds.max.y - top;
         }
         top += shiftY;
         bottom += shiftY;
         centre = new Vector2((left+right)/2,(top +bottom)/2);
         velocity = new Vector2 (shiftX, shiftY);
     }
 }
 

}

And here is the script for the Cross-Hair, which is actually a 2D sprite that follows the mouse position in world-space:

public class Crosshair : MonoBehaviour {

 private Vector3 MouseCoords;
 public float MouseSensitivity = 0.1f;

 void Update () {

     GameObject crosshair = GameObject.Find ("crosshair");

     MouseCoords = Input.mousePosition;
     MouseCoords = Camera.main.ScreenToWorldPoint (MouseCoords);
     crosshair.transform.position = Vector2.Lerp (transform.position, MouseCoords, MouseSensitivity);

     print (MouseCoords);
 }

}

Comment
Add comment · Show 1
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 _Gkxd · Jul 19, 2015 at 06:18 PM 0
Share

I'm not quite sure what your "focus box" is.

If the size of your focus box is constant though, couldn't you just clamp the crosshair position within some distance of the player?

 // Pseudocode
 crosshairX = $$anonymous$$athf.Clamp(playerX - sizeX, playerX + sizeX, crosshairX);
 crosshairY = //...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Deadfrontier type crosshair 1 Answer

2D Orthographic Main Camera and UI 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Change the background color attribute of a camera in C#? 2 Answers

Camera setup question 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