In Runner Game I need to detected collions with Objects. I use CharacterController and OnControllerColliderHit(ControllerColliderHit hit):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mainPlayerScript : MonoBehaviour
{
void OnControllerColliderHit(ControllerColliderHit hit)
{
print(hit.gameObject.name);
}
}
But, if my character collide with some object in front of it - no collision detected! What'd the problem?
P.s. In the game I use the method: my Character always stay. All world moves around it. Is it the best way?
This is because you moved transform.position. use CharacterController.Move() instead.
Controller.Move(SomeMotion * Time.deltaTime);
The onControllerColliderHit only gets called if the controller collides with something while moving using the Charactercontroller.Move() function. Since your character never moves this won't be called.
If you still want the world to move you could try and use a raycast with set max distance to check if something is in front of your player.
Related
Assumptions / What I want to achieve
I am making an action game that moves only the x and z axes in 3D.
I'm trying to make this compatible with online multiplayer.The Player object has such a component.
Use Joystick of StandardAssets for OnlinePlayerController.cs,
There is code to move using the rigidbody velocity.
The code is written below.
The problem I am having
Lag always occurs when loading an online scene from another scene.
Applicable source code
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityStandardAssets.CrossPlatformInput;
public class OnlinePlayerController : MonoBehaviourPunCallbacks
{
public float speed;
[SerializeField]
GameObject Rotation; //Omitted because it has nothing to do with movement
void Update()
{
x = CrossPlatformInputManager.GetAxisRaw("Horizontal");
z = CrossPlatformInputManager.GetAxisRaw("Vertical");
rigd.velocity = new Vector3(x * speed, 0, z * speed);
}
}
What I tried
As you can see from this article, I tried to fix the lag, but there was no change.
The Player object has a Rigidbody only to use Rigidbody.velocity.
Gravity is ineffective and friction with the floor is high and will not slip.
Supplementary information
Unity2019.2.6f1
PUN2 Free
It's important to note that there is no lag when playing from the scene in the editor.
I think that Photon is not the cause, as in other questions, but I think Unity is the major cause. (For example, the previous scene is interfering, etc.)
This is my first time using PUN2, so I don't know much about this.
What should I do to solve this?
Sorry, everything was my mistake.
This code was running before the scene transition.
Time.fixedDeltaTime = 0.2f;
I'm creating an Asteroids clone for a project. I currently am using MoveTowards to have the asteroids move where the player was once they spawned. What's limiting me using this method is they pause once they reach that point, which I know is due to MoveTowards. Is there another method for having the asteroid track the player location once it spawns and then moving to and BEYOND that point? Was trying to search around and couldn't find a specific answer to my question
Here is what I currently have that's applicable to this question. If I need to supply any more code please let me know and I will.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroids : MonoBehaviour
{
public Vector3 playerPosition;
public Vector2 direction;
public int asteroidSpeed;
// Start is called before the first frame update
void Start()
{
playerPosition = GameManager.gm.player.transform.position;
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, playerPosition, asteroidSpeed * Time.deltaTime);
}
You want your vector to be BASED ON the line described by the initial point and the spawn point. Then, you want to move along that vector, but NOT move "to" the spawn point.
As the title suggests, I've literally just started learning Unity recently and I'm practising by making a side scrolling shooter. I've been following a Udemy course about it and (to my knowledge) I've been following the tutor's instructions to the letter, but at the point where he tests his and it works fine, by projectiles go straight through my enemies.
I'm a bit stumped at this point and so I thought I'd post here to see what you guys thought. I bet it's something really simple I've not done.
Please see my projectile code below:
using UnityEngine; using System.Collections;
public class DestroyEnemyAndProjectile : MonoBehaviour {
public GameObject WhiteExplosion;
public GameObject OrangeExplosion;
void Start()
{
}
void Update()
{
}
void OnCollisionEnter2D (Collision2D tempCollision)
{
if (tempCollision.gameObject.tag == "Collision")
{
spawnParticles(tempCollision.transform.position);
Destroy(tempCollision.gameObject);
}
}
void spawnParticles(Vector2 tempPosition)
{
Instantiate(WhiteExplosion, tempPosition, Quaternion.identity);
Instantiate(OrangeExplosion, tempPosition, Quaternion.identity);
}
}
Thanks for your help!
I did post a question to them which got a response, they suggested that perhaps the projectile is going too fast and check there was a Rigidbody 2D attached - both of which were already collect
make sure that the object you're colliding with has the tag "Collision", with the same capitalisation.
If it's not, you can do this by:
1. Selecting the GameObject to be collided with
2. In the top-right, select the Tag property
3. Add tag, click the plus and type in "Collision"
4. Select the GameObject again, and select the "Collision" tag from the Tag property dropdown
Otherwise, if that's not the issue. Make sure the projectile has a type of Collider2D component, and that the projectile, or the object being collided with, has a Rigidbody2D.
First I would like to know what the behaviour of this script curently is. Is the collision method being called, and the projectile goes through the enemy anyway? Or the collision method is not being called at all?
That being said, these are things that you should check in order for collision to work:
Make sure that the projectile and the enemy have Collider2D components attached to them.
Make sure that the projectile and/or the enemy (at least one of them must) have a Rigidbody2D attached to it.
Make sure that the layer of the projectiles and the layer of the enemies are set to collide in the collision matrix (You can find the collision matrix in Edit->ProjectSettings->Physics)
Make sure that the enemy GameObject has its tag set to "Collision".
P.S: Welcome to Unity where issues like this are, in fact, usually caused by something super simple that you probably missed.
I'm working on a 2D platformer game in Unity and i want have a Sonic the Hedgehog like spring, in which when you step on it, player will be bounced very high. What i have come up with so far is this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bouncing : MonoBehaviour
{
public Vector2 bouncingPower;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Player"))
{
other.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2 (bouncingPower.x, bouncingPower.y);
}
}
This works, but only for y-axis. I set up the force on for y and when the player is stepping on the bouncer it will be bounced high, depending on y value.
However, i also want to have bouncer be at diagonal position, so when the player collides with this kind of bouncer, the player gets thrown diagonally, but when i set up the x value aswell, it sort of works. Player only gets tilted a bit to the side and that's about it. I understand that setting these values only determines how strong would i like player to be bounced as opposed to setting the angle being thrown at.
What would be a good solution have player be thrown in, let's say, 45 degree? I would like it if the player can be bounced at x-axis only aswell, but not really a mandatory.
I know that this is not the best solution, indeed I would have done initially the same, put rigidbody velocity with X-Y vector.
But if you say that this do not works, you can try adding an effector to the "jumper".
Here is a good tutorial of how 2d effector's works.
https://www.youtube.com/watch?v=hgUqhsAzGWs
I am trying to make a 2D game in Unity and I was curious if there is a way to check if there WILL be a collision between 2 objects rather then telling me WHEN they actually collide.
For Example - I am making a Space Invaders game and I want to check if any one of the space invaders on the edges will collide with a edge before they actually move in a C# script. I coded in Game Maker Studio and I remember that function being available but I am having a hard time finding out if its possible in Unity. Is this possible in Unity?
Thanks for the help.
yes its possible, you would have to do some sort of raycasting in the direction that the gameobject is moving you can check This Link for Physics.Raycast to see how to go about doing what youd like
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10))
print("There is something in front of the object!");
}
}