How can I make my character be larger through a script even when he turns to his -x value?
I can make my character bigger but i have ran into a problem. When he turns left it changes his x value to -x which causes the addition to go the opposite way.
heres the code, i have a few other things going on but d_NewPosition is the scale of the character and the last if and else statement is what i tried. Im new to c# so I need alot of help with this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class cat : MonoBehaviour
{
public AudioSource someSound; public GameObject Player; Vector3 d_NewPosition;
Rigidbody2D rb;
float dirX, moveSpeed = 5f;
[SerializeField]
Text coinCounter;
[SerializeField]
GameObject coinMagnet;
int coinsNumber;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
d_NewPosition = Player.transform.localScale;
}
// Update is called once per frame
void Update()
{
dirX = Input.GetAxisRaw("Horizontal") * moveSpeed;
coinCounter.text = coinsNumber.ToString();
coinMagnet.transform.position = new Vector2(transform.position.x, transform.position.y);
if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
{
rb.AddForce(Vector2.up * 700f);
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(dirX, rb.velocity.y);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag.Equals("Coin"))
{
Destroy(col.gameObject);
coinsNumber += 1;
someSound.Play();
if (d_NewPosition.x >= 0)
{
d_NewPosition += new Vector3(.1f, .1f, 0);
}
else
{
d_NewPosition -= new Vector3(.1f, 0, 0);
}
}
}
}
Hi @$$anonymous$$xkee
Your question makes very little sense without seeing an image / sketch of what you are trying to do.
You have field, that you call "d_NewPosition", but you actually store to it your Player's scale - Not position?
Then you do:
if (d_NewPosition.x >= 0)
{
d_NewPosition += new Vector3(.1f, .1f, 0);
}
What do expect to happen?
You are modifying x and y of your Vector3 value?
But you don't seem to even apply this value back to your scale, or position.
And you only get your d_NewPosition in start, not later.
Please rephrase this question if possible, it is hard to understand.
im so sorry, basically what im trying to do is make my character larger. so i add .1 to his x and y scale. BUT when he turns to his left his x value turns to -x which makes him look left, and my problem with that is when he is looking to his negative x, it still adds .1 which makes it go from -1 to -0.9, ultimately making him smaller in his x value. what other info do you need? i need this to work
Your answer
