Hi, I’m a little new to unity so this might not be such a big problem.
I made this soccer game with four walls surrounding the field and goals on both sides.
The middle of the field is (X = 0, Y = 0, Z = 0)
When the ball’s Z coordinate becomes negative Z. Or (0, 0, -1) I want the ball to change to the color Red
When the ball’s Z coordinate becomes positive Z. Or (0, 0, 1) I want the ball to change to the color Blue.
I’m having problems trying to get the script to find my balls Z coordinate. I just typed transform.position.Z, but I’m pretty sure that’s now how you do it.
Can anyone help?
PS, I have images at the bottom. I hope they help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ballthings : MonoBehaviour
{
public Rigidbody rb;
public float ballPushSpeed;
public Color myColor;
public float rFloat;
public float gFloat;
public float bFloat;
public float aFloat;
//colors go from 0 to 1
//defult is 0
public Renderer myRenderer;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
myRenderer = gameObject.GetComponent<Renderer>();
ballChangeColor();
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
//Debug.Log("Entered");
if (collision.gameObject.tag == ("Player"))
{
transform.Translate(new Vector3(ballPushSpeed * Time.deltaTime, 0, ballPushSpeed * Time.deltaTime));
}
}
void ballChangeColor()
{
if (transform.position.Z < 0)
{
rFloat = 1;
bFloat = 0;
gFloat = 0;
aFloat = 1;
}
else if (transform.position.Z > 0)
{
rFloat = 0;
bFloat = 1;
gFloat = 0;
aFloat = 1;
}
else if (transform.position.Z == 0)
{
rFloat = 0.5f;
bFloat = 0.5f;
gFloat = 0;
aFloat = 0;
}
myColor = new Color(rFloat, bFloat, gFloat, aFloat);
myRenderer.material.color = myColor;
}
}




