- Home /
Need help with onclick scale script (c#)
I have a script for when i click the object expands in the x axis and once i let go it reduces back to the original size.
CODE
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
transform.localScale = transform.localScale + Vector2.Scale * speed * Time.deltaTime;
new Vector2(5.358479f, 0.5769206f);
}
}
}
how about using something like Input.GetmouseButtonUp to bring the object back to original size.
Answer by vogles · Jul 23, 2015 at 06:06 AM
For the GameObject to go back to its original scale, you'd have to do something like this:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public float speed = 10;
private Vector3 originalScale;
void Start()
{
originalScale = transform.localScale;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.localScale = transform.localScale + Vector2.Scale * speed * Time.deltaTime;
}
if (Input.GetMouseButtonUp(0))
{
transform.localScale = originalScale;
}
}
}
This way if you change the scale of the GameObject in the Inspector, it'll retain that scale when you let go of the mouse button.
i get the error Operator *' cannot be applied to operands of type
method group' and `float'
Oh, my bad. I didn't actually read your bit of code. That error is because of line 18. I simply copy and pasted the code you posted. The Vector2.Scale is a method, which cannot be multiplied. I'm not sure why you had that in there.
You probably want that line to be something like this:
transform.localScale = transform.localScale * speed * Time.deltaTime;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Destroy object with more clicks depending on Scale? 0 Answers
Distribute terrain in zones 3 Answers
2D sprites, Negative scaling flips my look at mouse 0 Answers
slide an object by using mouse. 0 Answers