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
1
Question by shahinexir · Aug 10, 2012 at 11:53 AM · camera2djumpfollowparenting

i want the camera stop follwing my player only on y axis when he jumps...

Hi everyone. Im new to scripting. Im writing this code for my player. It moves great, it jumps well, and ive attached the camera to my player to be a child of it, but the problem is that i dont want the camera to follow the player when he jumps. Actually i want the camera to remain on where it is right now on its y axis but still be a child of my player and follow it on x axis and i have to mention that its a 2d game and ive attached this script to my player. I really need your help. tnxx alot...

 var playerSpeed : int;
 var jumpHeight  : int;
 
 function Update () 
 {
     //1- childing the main camera to the player, so that it will follow the player transform.position
     // getting the transform of the main camera and put it in a new var.
     var cameraTransform = Camera.main.transform;
     
     // making the camera child of the player.
     cameraTransform.parent = transform; 
     
     //2- moving the player...
     // amount to move the player 
     amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
     
     // transform/translate the movement of the player...
     transform.Translate(Vector3.right * amtToMove);
     
     //3- player jump
     // player can countinusly jump when you hold the space. if its transform position in y axis is equal to 0.73(on the ground)
     if ( Input.GetKey("space") && transform.position.y == 0.73)
     {
         amtToJump = jumpHeight + Time.deltaTime;
         transform.Translate(Vector3.up * amtToJump );
     }
  
     // now here i want to add something that can prevent the camera to follow my player when he jumps! and i need your help :(
 
 }
   
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by shahinexir · Aug 10, 2012 at 08:28 PM

oh here we go :D i fixed it myself! :P i just added this function out of my update function after parenting the camera to the player,and just changed the value to what works...easyy :

 function LateUpdate()
 {
     Camera.main.transform.position.y = 2.77;  
 }


now theres another problem! i want the player rotate when i press for example left arrow,and countinue moving to the left direction.just like old super mario movement . when u press left it would rotate and walk,and when u hold right arrow it could walk at the opposit direction.now i want something like that.i wrote some roatation code but when i hold the left or right arrow it just rotates and stops moving.and also my camera is a child of my player,and when player rotates the camera rotates too! i want to stop that too. really need your helpp. tnx

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 BLarentis · Aug 11, 2012 at 03:11 AM 0
Share

$$anonymous$$y answer will resolve the problem of the camera rotation. I'll make some test here and soon i'll post the code to rotate the character along his movement direction.

avatar image BLarentis · Aug 11, 2012 at 03:23 AM 0
Share

Ok, use this code(if the script that control the character is attached into the player) when you press the right arrow: this.gameObject.transform.rotation = Quaternion.Euler(0,0,0); and use this one when you press the left arrow: this.gameObject.transform.rotation = Quaternion.Euler(0,180,0);

Hope it helps. Take care

avatar image
4

Answer by BLarentis · Aug 10, 2012 at 08:31 PM

Hello, what you can do is take the camera out of the player. Don't put it as a child of player. Inside the camera you can put a script that will follow the player only in X axis:

C#- Script

 using UnityEngine;
 using System.Collections;
 
 public class FollowPlayer : MonoBehaviour {
  
  public GameObject player;
  
  // Update is called once per frame
  void Update () {
  
  this.gameObject.transform.position = new Vector3(player.gameObject.transform.position.x,this.gameObject.transform.position.y,this.gameObject.transform.position.z);
  
  }
 }

Javascript:

 public var player : GameObject;
 
 function Update () {
 
  this.gameObject.transform.position = new Vector3(player.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z);
 
 }

After you drag the script inside the camera, just reference the player object.

Hope this helped you. Take care!

Comment
Add comment · Show 5 · 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 Bunny83 · Aug 11, 2012 at 03:46 AM 0
Share

Uhmm... Don't you think you can make it a bit more complicated?

The "this" keyword is redundant and is only required in some really rare cases. As long as you are within the class scope you can access any method, property or variable.

Every component has access to the shortcut properties like ".transform", so there's no need to use gameObject

Vector3 is a struct and therefore a value-type, you can simply assign Vector3 values to other variables:

Your player variable already points the the gameObject, so there it's again useless.

 transform.position = player.transform.position;

Finally you should use variable types that are most useful for your usage. GameObject is not a very useful type since you can't do much with it. When you just need the Transform component of a GameObject, make the variable a Transform, so you can directly access the position.

 public Transform player;
 
 void Update()
 {
     transform.position = player.position;
 }

Btw: No one prevents you from doing something like that:

 transform.gameObject.transform.gameObject.transform.transform.position

But it doesn't make much sense and just slows everything down.

avatar image BLarentis · Aug 11, 2012 at 03:54 AM 0
Share

Yep, i know about the transform.gameObject.transform.gameObject.transform.transform.position i just wanted to specify the most so shahinexir can understand what kind of value i'm acessing. And your method of transform.position = player.position wont prevent the camera to follow while the player jumps, that its the problem that he is facing. Thats why i specified the x,y,z parameters into a new Vector3, so that i can track the player in x, and keep the camera position in y and z.

avatar image Bunny83 · Aug 11, 2012 at 04:14 AM 0
Share

Yes, sorry, but those little differences in these long lines can be easily skipped by accident.

In UnityScript you can simply do it like this:

 transform.position.x = player.transform.position.x;

However this won't work in C#.

btw:
I don't get why the more complicated and slower way should be easier to understand... ;)

avatar image Paulo-Henrique025 · Aug 22, 2012 at 09:50 PM 0
Share

Hey @Bunny83 can you confirm that accessing a variable this way: this.gameobject.transform.position is slower than just doing transform.position?

I prefer to use the this.gameobject or this.transform to access my stuff so the code gets more readable.

avatar image Yllier · May 07, 2014 at 03:41 PM 0
Share

The script that you posted was in Update. This caused jittery movement, most camera scripts should be put in LateUpdate.

avatar image
0

Answer by shahinexir · Aug 11, 2012 at 09:50 AM

hey guys realy apreciate for your helps but i think im done with the camera section,as i mentiond earlear by using the late update function it works well right now.

AND also tnx alot BLarentis but still it doesnt work :(( let me give u my latest player script:

var playerSpeed : int;

var jumpHeight : int;

function Update () {

//1- childing the main camera to the player, so that it will follow the player transform.position

// getting the transform of the main camera and put it in a new var.

var cameraTransform = Camera.main.transform;

// making the camera child of the player.

cameraTransform.parent = transform;

//2- moving our player...

// amount to move the player

  amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
  

// transform/translate the movement of our player...

  transform.Translate(Vector3.right * amtToMove);

//------------------------------------------------------------------------

//3- player rotation...!

  if (Input.GetKey("left") || Input.GetKey("a"))
    {
        this.gameObject.transform.rotation = Quaternion.Euler(0,0,0); 
    }
         
  if (Input.GetKey("right") || Input.GetKey("d"))
    {
        this.gameObject.transform.rotation = Quaternion.Euler(0,180,0);
    }
      



//--------------------------------------------------------------------------

//4- player jump

// player can countinusly jump when you hold the up button. if its transform position in y axis is equal to 0.73(on the ground)

if ( Input.GetKey("up") && transform.position.y == 0.73) {

     amtToJump = jumpHeight + Time.smoothDeltaTime ;
     
     transform.Translate(Vector3.up * amtToJump );
     

   }
   
 

}

 //5- camera follow
 
 // this function lets the camera be parented to the player but prevent it from moving along the y axis when the player jumps.

function LateUpdate()

{

Camera.main.transform.position.y = 2.77;

}

now i can move,jump,and the camera works well.but im facing new problems . its been about a day that im thinking to fix this.now i cant rotate, even if i rotate the camera rotates too.but i dont want that happen.and also ive got a cube like wall and i want my player to stop passing through it when it hits the wall.now i have box colider on both my player and wall with is trigger off,and have rigid body with use gravity on and is kinemati off on my player and wall. i think they colide cuz the wall doesnt move but my player suddenly jumps to the sky when it hits the wall! i dont know whats the problem. really need your help.and really apreciate that. and by the way :D forgive me for my bad english ! ;)

Comment
Add comment · Show 20 · 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 BLarentis · Aug 11, 2012 at 06:15 PM 0
Share

Hello again, my code for the player rotation only works if the camera isn't child of the player. If you use my solution to move the camera then the rotation will work. Cause that is the problem(in this case) to have an object child of another, everything that you will do with the parent the child will do the same. For the collision to work you only need a rigidbody on the player. Unless you want to move the wall or something like that. Take care!

avatar image shahinexir · Aug 12, 2012 at 07:11 AM 0
Share

oh tnx man . i fixed the camera rotation problem . your right . but about the colision still have some problems . u mean remove the rigid body from all the wall cubes exept my player ? if so , then i wont be able to have colision when my player shoots projectile to the wall,and they wont colide together unless the wall have rigid body .

avatar image Bunny83 · Aug 12, 2012 at 11:34 AM 0
Share

You don't need a rigidbody on objects that do not move. A Rigidbody implements collision detection for moving objects. Static objects are just colliders and as long all moving objects have either a rigidbody or are a charactercontroller those static objects don't need a rigidbody. Every object that is moved around should have either a rigidbody or a charactercontroller.

avatar image shahinexir · Aug 13, 2012 at 02:24 PM 0
Share

so bunny83 what u sayin is to remove all the floor cube and other wall cubes rigid bodies.and just have rigid body on my player ? what about the is trigger ? and is kinematic ? i want my player to recognize the floor and a cube as a wall and stop moving when it hits them . or colide with them .

avatar image BLarentis · Aug 13, 2012 at 02:29 PM 0
Share

For the floor you don't need to have a rigid body(since he will not move), only a collider will work. If you want the wall cubes to receive impact from projectiles then all of them will need a rigid body. Let me try to make this example works in here and i'll send you how to make it. Just to confirm, you want a wall made of cubes, that won't let the player pass, but if you shoot the wall, it will fall ?

Show more comments
avatar image
0

Answer by chesterhilly · May 08, 2015 at 01:25 PM

the script I used would always snap to a set position, and I tried many things to fix it. In the end I just tried using this script:

 function LateUpdate () {
     transform.position.y = 3.05;
     transform.position.z = -6.94;
 }


Make sure it is a late update or else it will not work! When the object your the camera is following, the camera should remain in the place you said in the script, but the x axis will still move

(I also made the axis as 3.05 and -6.94 as that because that was the right camera positon)

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

13 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

Related Questions

How do I lock Y Axis movement for Parented Camera? 3 Answers

Camera script 1 Answer

Cinemachine 2d camera problem 0 Answers

Shaky player effect when I apply a damping camera follow script? 1 Answer

Camera dont move whit Smooth Follow 2D 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