Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Collison with Objects to change colour

Discussion in 'Scripting' started by LostSoul19781, Mar 29, 2017.

  1. LostSoul19781

    LostSoul19781

    Joined:
    Mar 29, 2017
    Posts:
    1
    Hi,

    Little frustrated as a novice user and C# newbie. I have been trying to program script that will allow an object to change colour when impacted by an external force (Public Class PhysicalExplosion.force), but don't understand where I am going wrong with the collision script.

    Desperately seeking help!

    Lostsoul

    Script below --


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Impact : MonoBehaviour
    {

    GameObject Ball;

    private Rigidbody rb;
    private Vector3 relativeVelocity;

    // Use this for initialization
    void Start()
    {
    rb = GetComponent<Rigidbody>();
    Ball = GameObject.Find("Ball");
    }
    void FixedUpdate()
    {
    rb.AddForce(relativeVelocity);
    }
    //public Vector2 Impact.relativeVelocity;
    public void OnCollisionEnter2D(Collision2D col)
    {
    Renderer rend = GetComponent<Renderer>();
    float force = col.relativeVelocity.magnitude;

    if (force > 0 && force < 4)
    {
    rend.material.shader = Shader.Find("Specular");
    rend.material.SetColor("_SpecColor", Color.green);
    }
    if (force > 4 && force < 6)
    {
    rend.material.shader = Shader.Find("Specular");
    rend.material.SetColor("_SpecColor", Color.yellow);
    }
    if (force > 6 && force < 12)
    {
    rend.material.shader = Shader.Find("Specular");
    rend.material.SetColor("_SpecColor", Color.red);
    }
    if (force > 12)
    {
    rend.material.shader = Shader.Find("Specular");
    rend.material.SetColor("_SpecColor", Color.black);
    }

    }
    }
     
  2. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    57
    1, You are using OnCollisionEnter2D. Do you also use Rigidbody2D and 2D colliders? Don't you mix 2D components with 3D components (it won't work)?
    2. Is the OnCollisionEnter2D called ever? Put a Debug.Log("whatever") or print("whatever") to the top of the OnCollisionEnter2D method to see whether it's called. Also check what value is in force variable after you set it.
    3. Edge cases are not treated (however it's probably not the root of the problem here).
    you test
    Code (CSharp):
    1. if (force > 0 && force < 4)
    2. {...}
    3. if (force > 4 && force < 6)
    4. {...}
    In case force is 4, it won't be catched. So in the second condition needs to be checked whether it's greater or equal.
    5. Try to eliminate repeating duplicate code. E.g.
    Code (CSharp):
    1. rend.material.shader = Shader.Find("Specular");
    can be set once before you start testing of force variable. Also you might use Color to store color in the if conditions and then set the color of material at the end of the method.
     
  3. JoshGreen

    JoshGreen

    Joined:
    Mar 16, 2017
    Posts:
    28
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Impact : MonoBehaviour
    {

    GameObject Ball;

    private Rigidbody2D rb;
    public Vector2 relativeVelocity;

    // Use this for initialization
    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    Ball = GameObject.Find("Ball");
    }
    void FixedUpdate()
    {
    rb.AddForce(relativeVelocity);
    }
    //public Vector2 Impact.relativeVelocity;
    public void OnCollisionEnter2D(Collision2D col)
    {
    Renderer rend = GetComponent<Renderer>();
    float force = col.relativeVelocity.magnitude;

    if (force > 0 && force < 4)
    {
    rend.material.SetColor("_Color", Color.green);
    }
    if (force > 4 && force < 6)
    {
    rend.material.SetColor("_Color", Color.yellow);
    }
    if (force > 6 && force < 12)
    {
    rend.material.SetColor("_Color", Color.red);
    }
    if (force > 12)
    {
    rend.material.SetColor("_Color", Color.black);
    }

    }
    }


    Just corrected some of your stuff (it works in 2D fine now, you will have to put a 2D collider on your objects now), you need to either use 2D or 3D physics. I have presumed you are working in 2D but just swap out all the 2D to 3D rigidbodies/onCollide if you want it in 3D.