Collision detection with character controller in Unity 3d - c#

I have my player which uses character controller for moving. I placed a sprite in the scene and I'd like for when my player collides with the sprite to disable the sprite, like if the player grabs the sprite (which is Doom's 64 chainsaw).
The sprite's collisions of course work well with everything, but not with the player. How can I get proper collision between them?

You could do it like this:
1-Attach "Pickable" script to the sprite.
2-Attach "Player" script to the character controller.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickable : MonoBehaviour
{
public float radius = 1f;
private void Start()
{
SphereCollider collider = gameObject.AddComponent<SphereCollider>();
collider.center = Vector3.zero;
collider.radius = radius;
collider.isTrigger = true;
}
}
Here is the other script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Pickable pickable = other.GetComponent<Pickable>();
if(pickable != null)
{
Destroy(other.gameObject);
}
}
}

Related

OnTriggerEnter in another script

Tell me please. I have a character. The character has a child element - sphere collider. This sphere is a trigger. There is a script that is located on the Terrain. How can I call the OnTriggerEnter function in this script which is located on the character sphere?
Terrain script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public Transform Human;
// Update is called once per frame
void Update()
{
var humanTrigger = Human.Find("Trigger");
humanTrigger OnTriggerEnter(Collider other) {
print(other);
}
}
}

Unity clone won't appear

I'm new to coding and i'm trying to spawn a clone of a ball in unity, but it won't appear.
It does spawn at the spawn point but it doesn't appear.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public GameObject ball;
public Transform spawnPoint;
// Start is called before the first frame update
void Start()
{
SpawnBall();
}
// Update is called once per frame
void Update()
{
}
void SpawnBall()
{
Instantiate(ball, spawnPoint.position, Quaternion.identity);
}
}
Edit: it's a 3d game and the mesh renderer is enabled.
Okay nevermind: i just redid the ball and it worked.
¯_(ツ)_/¯

Unity C# problem with camera, camera goes white

I made a script for my camera for following the player.When I play the game, game view goes white, even if on scene view all is fine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos;
}
}
The problem might be that the player is blocking the camera (because the camera is inside the player). Try adding some offset by adding a Vector3 as a variable and adding it on to the transform.position.
The offset could be used so that the camera is in front of the player, or in a third person angle.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
public Vector3 offset;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos + offset;
}
}
Hope this helps.

Unity 3D how to aim a weapon to a raycast hit

I have a weapon which shoots projectiles and i try to rotate the weapon towards a raycast hit. I attached two scripts too my weapon one for shooting and one for aiming the shooting script works fine.
Here my weapon script which inistiate my projectiles:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
using UnityEngine.AI;
using Random = UnityEngine.Random; // |source: https://community.gamedev.tv/t/solved-random-is-an-ambiguous-reference/7440/9
public class weapon_shooting : MonoBehaviour
{
//deklariere projektil |source: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
public GameObject projectilePrefab;
private float projectileSize = 0;
public Rigidbody rb;
public float projectileSpeed = 50;
public float projectileSizeRandomMin = 10;
public float projectileSizeRandomMax = 35;
void Update()
{
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1"))
{
//random sized bullets
projectileSize = Random.Range(projectileSizeRandomMin, projectileSizeRandomMax);
projectilePrefab.transform.localScale = new Vector3(projectileSize, projectileSize, projectileSize);
// Instantiate the projectile at the position and rotation of this transform
Rigidbody clone;
clone = Instantiate(rb, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection(Vector3.forward * projectileSpeed);
}
}
}
So then i tried to cast a ray from the middle of the screen to the mouse position and then rotate my weapon towards the point where the ray collides with my world. But when i run it it shoots in all directions :=o (no errors) need some help to figure this out :)
here is my weapon aiming script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weapon_aiming : MonoBehaviour
{
public Camera camera;
private bool ray_hit_something = false;
void Update()
{
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
ray_hit_something = true;
} else
{
ray_hit_something = false;
}
if (ray_hit_something == true) {
transform.LookAt(Camera.main.ViewportToScreenPoint(hit.point));
}
}
}
Just use LookAt(hit.point) as LookAt expects a position in world space and hit.point is already in world space:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weapon_aiming : MonoBehaviour
{
public Camera camera;
private bool ray_hit_something = false;
void Update()
{
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
ray_hit_something = Physics.Raycast(ray, out hit);
if (ray_hit_something) {
transform.LookAt(hit.point);
}
}
}

Some bullets doesn't show in multiplayer mode

I'm developing a shooting game in Unity and I have a problem with multiplayer mode when using Unity Multiplayer Platform(Unity Servers). My bullets are using Network Identity and Network Transform.
Some bullets doesn't show up on the side that shoots them but they are visible in other side and the damage is taken.
Here's my shooting script:
[Command]
public void CmdShoot(){
this.transform.FindChild("gun").SendMessage ("CmdGunshoot");
}
Here's my gun script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class shoot : NetworkBehaviour {
public GameObject bulletPref;
public int power;
private Transform spawner;
public int damage;
void Start () {
spawner = this.transform.FindChild("Bspwaner");
}
[Command]
public void CmdGunshoot (){
GameObject bull = Instantiate(bulletPref, spawner.position, Quaternion.identity) as GameObject;
bull.GetComponent<bullet>().dmg = damage;
bull.gameObject.transform.GetComponent<Rigidbody>().AddForce(spawner.right * power);
NetworkServer.Spawn (bull);
Destroy(bull, 2.0f);
}
}
Here's my bullet script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class bullet : NetworkBehaviour {
public GameObject ie;
public int dmg;
void OnCollisionEnter(Collision other){
Instantiate(ie, transform.position, Quaternion.identity);
NetworkServer.Destroy (this.gameObject);
if (other.gameObject.tag != "Player")
return;
other.gameObject.SendMessage ("takeDamage", dmg);
}
}
What the problem can be?
Thanks in advance,

Categories