The Object moves, but it doesn't change its direction and i don't know why.
Thats the code for the object named 'zackenblock':
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zackenblock_move : MonoBehaviour
{
public static bool col = false;
public float speed = 10f;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
if (zackenblock_col.collosin != col)
{
col = zackenblock_col.collosin;
}
}
// Update is called once per frame
void FixedUpdate()
{
if (col == false){
transform.Translate(Vector3.right * Time.deltaTime * speed);
}
else
{
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
/*if (Input.GetKey(KeyCode.M))
{
col = true;
}*/
}
void OnCollisionEnter(Collision collision){
//Check for a match with the specified name on any GameObject that collides with your GameObject
if (collision.gameObject.name == "Level1_part2")
{
//If the GameObject's name matches the one you suggest, output this message in the console
Debug.Log("Do something here");
col = true;
}
else if (collision.gameObject.name == "Level")
{
//If the GameObject's name matches the one you suggest, output this message in the console
Debug.Log("Do something here");
col = false;
}
}
/*IEnumerator waiter_not_that_waiter_just_waiter(){
yield return new waitforseconds(3f);
//my code here after 3 seconds
transform.Translate(Vector3.right * Time.deltaTime);
yield return new waitforseconds(3f);
transform.Translate(Vector3.left * Time.deltaTime);
}*/
}
And thats the code from the tilemap:
using System.Security.Cryptography;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zackenblock_col : MonoBehaviour
{
public static bool collosin;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.M))
{
collosin = true;
}
}
void OnCollisionEnter(Collision collision){
//Check for a match with the specified name on any GameObject that collides with your GameObject
if (collision.gameObject.name == "zackenblock")
{
//If the GameObject's name matches the one you suggest, output this message in the console
Debug.Log("Do something here");
collosin = true;
}
/*else if (collision.gameObject.name == "Level")
{
//If the GameObject's name matches the one you suggest, output this message in the console
Debug.Log("Do something here");
collosin = false;
}*/
}
}
Here are some screenshots:
Screenshot of unity and the object:
Screenshot of unity and the tilemap:
Screenshot of unity and the tilemap2:
The object should move to right and when it collides then move left.
It only moves right non-stop.
The major problem with your code is that you are misspelling. Change 'collosin' into 'collision'. And change:
void OnCollisionEnter(Collision collision)
into
void OnCollisionEnter2d(Collision2d collision)
Other than that it seems like your code is fine.
Use OnCollisionEnter2D because you are making a 2D game
private void OnCollisionEnter2D(Collision2D col)
{
}
also check out OnCollisionStay2D/exit if the collisions tend to stick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.