Quantcast
Channel: Questions in topic: "clone"
Viewing all articles
Browse latest Browse all 508

How can i scale down a 2d sprite via script

$
0
0
I'm working on a little game, the mechanics are not important, but when i jump up and hold down S, my player model will split into a total of 5, giving it the illusion that it's broken into more characters, as this happens i want all of the clones including the main character to scale their size down. is this possible? I should also mention i am an amateur programmer, and i'm currently using C# If needed here is my main character script. [code]using UnityEngine; using System.Collections; public class PlayerControl : MonoBehaviour { [HideInInspector] public bool facingRight = true; // For determining which way the player is currently facing. [HideInInspector] public float moveForce = 365f; // Amount of force added to move the player left and right. public float maxSpeed = 5f; // The fastest the player can travel in the x axis. public float jumpForce = 1000f; public float downForce = -1000f; // Amount of force added when the player jumps. public bool canbreak = false; public Rigidbody2D projectile; public float maxClones = 5; void Update() { } void FixedUpdate () { // Cache the horizontal input. float h = Input.GetAxis("Horizontal"); // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet... if(h * rigidbody2D.velocity.x < maxSpeed) // ... add a force to the player. rigidbody2D.AddForce(Vector2.right * h * moveForce); // If the player's horizontal velocity is greater than the maxSpeed... if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed) // ... set the player's velocity to the maxSpeed in the x axis. rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y); // If the input is moving the player right and the player is facing left... if(h > 0 && !facingRight) // ... flip the player. Flip(); // Otherwise if the input is moving the player left and the player is facing right... else if(h < 0 && facingRight) // ... flip the player. Flip(); // If the player should jump... if(Input.GetKeyDown(KeyCode.Space)) { rigidbody2D.AddForce(new Vector2(0f, jumpForce)); } if(Input.GetKey(KeyCode.S)) { rigidbody2D.AddForce(new Vector2(0f, downForce)); canbreak = true; split (); } } void Flip () { // Switch the way the player is labelled as facing. facingRight = !facingRight; // Multiply the player's x local scale by -1. Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } void Shrink () { Vector3 theScale = transform.localScale; theScale.x *= 1; transform.localScale = theScale; } void split() { if(canbreak == true) { do { Rigidbody2D clone; clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody2D; }while (maxClones < 5.0); } } } [/code]

Viewing all articles
Browse latest Browse all 508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>