Make the character move a certain direction on collision

Hi there, I am trying to make my character move in the positive y direction when it collides with an object, in this case the wall.

For some reason this does not work at all, even the debug.log doesn’t print out. How do I make this work?

I am relatively new to javascript coding and so I’m sorry if it is a really easy thing to fix.

Code sample below:

function OnCollisionEnter (collision : Collision) {
if (collision.gameObject.tag == "Wall" && Input.GetKey(KeyCode.LeftControl)) {
			
Debug.Log("Collided");
var pushDir = Vector3 (0, 1, 0);
		
gameObject.rigidbody.velocity = pushDir;
}
}

Did you attached a Rigidbody to the object that collide with the wall?

The object need a Rigidbody and the wall a collider when both enter in the trigger then the action start.

Here you go, it’s in C#. I tested with a character that has a rigidbody controller and it works well.

using UnityEngine;
using System.Collections;

public class pushBack : MonoBehaviour {
	public int pushDir = 10;
	public int pushBackSpeed = 1;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	void OnCollisionEnter (Collision collision) {
		if (collision.gameObject.tag == "Wall" ) {
			
			Debug.Log("Collided");
			
			transform.position = transform.position +
				transform.forward * -1 * pushBackSpeed;
			//gameObject.rigidbody.velocity.y = pushDir;
		}
	}
}