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 alfchee · Sep 27, 2012 at 07:16 AM · charactercontrollerscaleruntimeitween

How I can change the scale of my CharacterConroller in runtime?

Hello everybody. I have my charactercontroller, is a 2D game, but I want to change it scale in runtime, and I see two problems: a) When I change the localScale, using lerp, if I make it smaller it stand on the air then fall and walk; if I make it grow, grow through the soil, and then fall over the emptiness. b) I make click over a place and create a targetPoint, but some times the character controller moves to a different place.

I gonna paste my code, maybe you could help me.

   if(Vector3.Distance(new Vector3(0,0,targetPosition.z),new Vector3(0,0,firstPlane.z)) < 0.05f)
        //scale = Mathf.Lerp(transform.localScale.x,firstPlaneScale.x,Time.fixedDeltaTime * smoothScale);
        iTween.ScaleTo(transform.gameObject,firstPlaneScale,10f);
  else if(Vector3.Distance(new Vector3(0,0,targetPosition.z),new Vector3(0,0,secondPlane.z)) < 0.05f)
       //scale = Mathf.Lerp(transform.localScale.x,secondPlaneScale.x,Time.fixedDeltaTime * smoothScale);
       iTween.ScaleTo(transform.gameObject,secondPlaneScale,10f);
  else if(Vector3.Distance(new Vector3(0,0,targetPosition.z),new Vector3(0,0,thirdPlane.z)) < 0.05f)
       //scale = Mathf.Lerp(transform.localScale.x,thirdPlaneScale.x,Time.fixedDeltaTime * smoothScale);
       iTween.ScaleTo(transform.gameObject,thirdPlaneScale,10f);
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 AlucardJay · Sep 29, 2012 at 07:25 AM 0
Share

check this answer : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by justinl · Sep 28, 2012 at 03:53 AM

This can get to be a bit tricky. I've had the same problems when implementing my player crouch. As you scale the character controller you need to also move the center of it so that you don't get things like floating up into the air or sink into the ground.

I did not animate my CharacterController and instead I simply snap it in 1 frame as I've found it drastically decreased the complexity and I had no need for the character controller collision mesh to animate.

I notice in your code you are scaling your entire gameObject, but I think that's probably not how you want to do it since that would also scale the mesh of the player. You'll want to grab an instance of the controller with:

CharacterController myController = transform.gameObject.getComponent();

Then you can controller the scale and height with:

myController.height;  
myController.center;

The trick is that when you snap the new scale of the controller, you want the new center to be exactly in the same place as your original center. This will avoid floating into the air or falling into the ground. Here is my crouching code if you're interested:

void Crouch(){ charController.height -= crouchHeight; //original value was 2, but crouchHeight = 1 charController.center -= new Vector3(0,crouchHeight/2, 0); //original center was 0 but now is 1. }

void Stand(){ charController.height += crouchHeight; charController.center += new Vector3(0,crouchHeight/2, 0); }

Comment
Add comment · Show 3 · 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 alfchee · Sep 28, 2012 at 09:09 PM 0
Share

Thanks for your help... I guess I understand what you are explaining, I'm gonna try to do something like that. And by the way, I'm working with sprites, and really I want to make the sprite at the same size than my controller.

avatar image justinl · Sep 29, 2012 at 01:05 AM 0
Share

No problem. I haven't worked that much with sprites in Unity, but I still feel that you probably wouldn't want to scale the sprite card itself and probably you don't even need to scale the sprite card itself. If you did scale the card, texture mapping could get weird and if you animated the scale ins$$anonymous$$d of snapped it, you'd have to do some pretty complex reverse scaling on your texture so that it didn't look like it started to squish. Just my 2 cents.

avatar image alfchee · Oct 01, 2012 at 09:40 PM 0
Share

Well, but at really I want to re-scale the sprites, for the moment I'm not having problems with the textures, I'm using 2DToolkit to handle the sprites and make animations with them, but I want to move my character not only in X-axis but in Z-axis, that's my actually problem.

avatar image
0

Answer by rokyed · Sep 29, 2012 at 07:24 AM

you can also make one game object with a character controller and only modify the character controller height and radius like here:

 var controller : CharacterController;
 function Start(){
 controller = GetComponent(CharacterController);
 controller.height = 2.0;
 controller.radius = 0.3;
 }

and the graphics stock it in the gameobject with the character controller and scale it as u wish .. i think that works , if not u can add something like a jump for the character controller so like that it won't gitch trough the soil

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 alfchee · Oct 01, 2012 at 09:36 PM 0
Share

I guess I'm getting the trick, Im gonna try to make it work making an object, adding the charactercontroller to it and the scripts, and my animatedSprite like a child of the first gameObject, so I can treat the charactercontroller and the sprite by separate... I'm gonna tell you if works, thanks

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

12 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

Related Questions

Setting the Unity editor scale 3 Answers

itweenpath scale and position 0 Answers

iTween ScaleTo + gravity == halting falling motion 0 Answers

Character Controller surpassing Collider with iTween 0 Answers

How expensive is scaling in 2D unity? Is it better to pre-scale? 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