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 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 him (on x and y, it's 2d gameplay) But when the player jump, i want the camera continue to follow him but not following him on his jump movement. I don't if it's clear ! :/ Is there a way to achieve 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

have you some script that controls camera? just disable or clamp there it's Z position

avatar image Eldh · Aug 05, 2012 at 01:11 PM 0
Share

there is no z axis, it's 2D gameplay I edited my question !

avatar image ScroodgeM · Aug 05, 2012 at 01:15 PM 0
Share

you should note that in question 8)

avatar image ScroodgeM · Aug 05, 2012 at 01:17 PM 0
Share

what do you mean in 'it's not working perfectly'

avatar image Eldh · Aug 05, 2012 at 01:21 PM 0
Share

Cause the player continue moving on y axis even if he is not jumping. It's a diagonal direction, so the camera stop folowing him on y, but the player still move on this axis. I want the camera to just don't move when he is jumping, but still following him on his base movement.

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 think I understood. This 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 while he is jumping.

Before putting any code, this system should work like this:

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

You take this y value and assign it to the camera position, and the camera should move diagonally on the ground, while 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 this code and let us know you did it.)

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

Remember, you need to call this 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

Yeah you understood it well ! Thx you man, it's working ! If i could i would "thumb up you"... but i can't... ^^ thx again !

avatar image
0

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

ok.. if it's just while jumping that's the issue, you need a separate var for that, something like:

 void Update() 
 { 
  if(Input.GetButton("Jump"))//or however you choose to determine this 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 this also.. just an example
 jumping = false; 
 }

Of course, this 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, something 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.

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

Mouse Orbit + Move Object + Follow Problem 1 Answer

Camera not moving on mobile 0 Answers

Camera focusing on an object specified in a script 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