• 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 Eldh · Aug 05, 2012 at 01:00 PM · cameramovementpositionjumpfollow

how to stop the camera to follow the player on his jump movement ?

So the player constantly move on a diagonal direction, and the camera follow $$anonymous$$m (on x and y, it's 2d gameplay) But when the player jump, i want the camera continue to follow $$anonymous$$m but not following $$anonymous$$m on $$anonymous$$s jump movement. I don't if it's clear ! :/ Is there a way to ac$$anonymous$$eve that ? Edit : my first atempt was that, but cause the player is always moving, it's not working perfectly

 using UnityEngine;

using System.Collections;

public class cameraScript : MonoBehaviour {

 public Transform target;
 public CharacterController controller;
 private int offSetz = -100;
 public float offSetx;
 public float offSety;
 private float lockPos;

 void Start()
 {

 }


 void Update() 
 { 
     if (controller.isGrounded)
     {
         transform.position = new Vector3(target.transform.position.x + offSetx, target.transform.position.y + offSety, offSetz);
         lockPos = target.transform.position.y;
     }
     else {

             transform.position = new Vector3(target.transform.position.x + offSetx, lockPos + offSety, offSetz);
         
         }
 }

}

Comment
Add comment · Show 6
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 ScroodgeM · Aug 05, 2012 at 01:02 PM 0
Share
avatar image Eldh · Aug 05, 2012 at 01:11 PM 0
Share
avatar image ScroodgeM · Aug 05, 2012 at 01:15 PM 0
Share
avatar image ScroodgeM · Aug 05, 2012 at 01:17 PM 0
Share
avatar image Eldh · Aug 05, 2012 at 01:21 PM 0
Share
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer Wiki

Answer by RodrigoSeVeN · Aug 05, 2012 at 04:51 PM

I t$$anonymous$$nk I understood. T$$anonymous$$s is more of a suggestion than the best solution.

As far as I get it, you need to keep the camera's x axis aligned with the player at all times, and only when he jumps, you need the y axis to stay on the surface of the ground, instead of "jumping" with the player.

The best suggestion I have, is to fire a ray from the player position, only when he is jumping, towards the ground, so you can get a position on the ground to use, instead of the y axis of the player w$$anonymous$$le he is jumping.

Before putting any code, t$$anonymous$$s system should work like t$$anonymous$$s:

You fire the ray during the player jump(when not grounded). You will get a vector3(the $$anonymous$$t.point), but you will only need the y value. To make it work correctly, you need to add another value to t$$anonymous$$s result, that being the approximate height of the player(t$$anonymous$$s height is actually the distance of $$anonymous$$s y position in relation to the ground he is touc$$anonymous$$ng, let me know if t$$anonymous$$s is not clear for you).

You take t$$anonymous$$s y value and assign it to the camera position, and the camera should move diagonally on the ground, w$$anonymous$$le the player is jumping.

Now for the related code. You can call a function on the camera script that only sets the y value when the player is jumping(let the camera know when it happens). Also, I'm considering your camera already have the player as a target and that your ground has a collider, so here you go.

(I only work with Javascript, but it won't be hard to translate. In case someone feels like doing so, please edit t$$anonymous$$s code and let us know you did it.)

 var target:Transform; //The player
 var pHeight:float=0; //The player "height", play with t$$anonymous$$s to see if you need it
 function AlignIfJumping(){
     var $$anonymous$$t:RaycastHit;
     if(Physics.Raycast(target.position, -Vector3.up, $$anonymous$$t)){
         var tempYPos:float=$$anonymous$$t.point.y+pHeight;
         transform.position.y=tempYPos;
     }
 }

Remember, you need to call t$$anonymous$$s function on the camera, only when the player is jumping and right after the camera do it's auto alignment with the player position, so the y axis position is changed correctly before the frame ends.

Comment
Add comment · Show 1 · 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 Eldh · Aug 05, 2012 at 06:50 PM 0
Share
avatar image
0

Answer by Seth-Bergman · Aug 05, 2012 at 02:16 PM

ok.. if it's just w$$anonymous$$le jumping that's the issue, you need a separate var for that, somet$$anonymous$$ng like:

 void Update() 
 { 
  if(Input.GetButton("Jump"))//or however you choose to determine t$$anonymous$$s based on your code
 jumping = true;

     if (jumping)
     {
         transform.position = new Vector3(target.transform.position.x + offSetx, target.transform.position.y + offSety, offSetz);
         lockPos = target.transform.position.y;
     }
     else {
 
             transform.position = new Vector3(target.transform.position.x + offSetx, lockPos + offSety, offSetz);
 
         }

  if(controller.isGrounded) // may need to adjust t$$anonymous$$s also.. just an example
 jumping = false; 
 }

Of course, t$$anonymous$$s all depends on when your camera should or shouldn't follow on y. If there is a certain scenario that is the issue, you could conversely simply account for that in a similar way, somet$$anonymous$$ng like:

 if (!diagonalMove && !controller.isGrounded)
 transform.position = new Vector3(target.transform.position.x + offSetx, lockPos + offSety, offSetz);
 else
 {
             transform.position = new Vector3(target.transform.position.x + offSetx, target.transform.position.y + offSety, offSetz);
             lockPos = target.transform.position.y;
         }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Getting Camera to Stop Following Player 1 Answer

Camera rotation around player while following. 6 Answers

Smooth Camera/Object Movement 1 Answer

In-Air movement relative to camera 1 Answer

How to make camera move backwards in relation to player (so behind player), regardless of y rotation of player and camera 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