Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by TheGreenHornut · May 10, 2017 at 01:21 PM · modelswitchkey pressedmorphing

How To Make Player Transform Into Ball and Back Using Space Key

I am creating a game where the player will be able to roll up into a ball and roll around in the level by pressing the space key. What I want to know is how to change the model of the player into a ball by pressing the space key and then back again when you press the space key a second time.

FYI I am using C#

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dardamavet · Jul 10, 2017 at 02:53 PM

Hey @TheGreenHornut, this was a fun experiment :)

This script will inflate your model to a ball and back.

In the attached script you can should set up the center of the model. The model will inflate around this point, so make sure it's inside your model. You can set the radius of the ball and the transitionTime.

The script will put each vertice in distance of radius from the center, with the same direction it had before.

I'm assuming you know how to handle space-bar pressing, but if not, please check out unity docs: https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

Enjoy

 using UnityEngine;
 
 [RequireComponent(typeof(MeshFilter))]
 public class inflateModel : MonoBehaviour {
     public float transitionTime = 5;
     public float radius = 10;
     public Vector3 center = Vector3.zero;
 
     private float timeLeft;
     private bool inflate = true;
     private Vector3[] originalVertices;
     private Vector3[] inflatedVertices;
     private Vector3[] currentVertices;
     private Mesh mesh;
 
     void Start()
     {
         timeLeft = transitionTime;
         mesh = GetComponent<MeshFilter>().mesh;
         originalVertices = mesh.vertices;
         inflatedVertices = new Vector3[originalVertices.Length];
         currentVertices = new Vector3[originalVertices.Length];
         for (int i = 0; i < originalVertices.Length; i++)
         {
             inflatedVertices[i] = (originalVertices[i] - center).normalized * radius + center;
             currentVertices[i] = originalVertices[i];
         }
     }
 
     void Update () {
         timeLeft -= Time.deltaTime;
         if (timeLeft <= 0)
         {
             timeLeft = transitionTime;
             inflate = !inflate;
         }
 
         float t = timeLeft / transitionTime;
         if (!inflate)
             t = 1 - t;
 
         for (int i = 0; i < currentVertices.Length; i++)
         {
             currentVertices[i] = Vector3.Lerp(inflatedVertices[i], originalVertices[i], t);
         }
         mesh.vertices = currentVertices;
         mesh.RecalculateBounds();
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Please explain a switch statement? 4 Answers

Button bool doesn´t work correctly weird 1 Answer

Help with footstep box triggers within another box trigger. 0 Answers

Imported Models Not Rendering Correctly? 5 Answers

How to create a plot system 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