Main camera only see the ground - unity3d

I make a 3d game.My gun only shoot the ground , even when I select other object to shoot. I have the ground at a rotation , it may affects the game? Should I add something to the main camera?
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
}
}
}
picture ground

Related

Quaternion.lerp and position are being weird

So, I'm trying to have the camera follow the player (a car) and rotate with the player (... a car). However, it seems that either the quaternion.lerp is sus, or the positioning is. I've tried localPosition, but it doesn't really have an effect. It's rotating with the player, but it stays like... on one side of the player. Here is what I mean:
https://imgur.com/a/TQJOQ2X
and then
https://imgur.com/a/4FKm709
here's the code:
public class CameraMove : MonoBehaviour
{
[Header("References")]
[SerializeField] Transform player;
[Header("Attributes")]
[SerializeField] float camDelayIntensity = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.localPosition = player.position + new Vector3(0, 3, -8);
Quaternion endPos = Quaternion.LookRotation(player.forward);
transform.rotation = Quaternion.Lerp(transform.rotation, endPos, camDelayIntensity);
}
}

How to get 2D rigid body to move in unity

I am fairly new to C# and unity and I am trying to make a square jump with a Vector2 but for some reason the square wont move. Here is the code for you to see:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float Jump;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump = Input.GetAxis("Jump");
if (Jump == 1)
{
new Vector2(0, 1);
}
}
}
try this
//player must have a rigidbody2D and a box colider
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
}
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}

2D Player Shaking When Platforms Move Up and Down?

For some reason, my player character shakes up and down when the platform moves up and down, which prevents the player from jumping because the player is not grounded. I tried several things including adding a kinematic rigidbody to the platform and trying to make the player a child of the platform after landing on the platform but nothing has worked so far. Any help would be appreciated. Thank you!
Here is my code:
public class MovingPlatform : MonoBehaviour
{
private Vector3 posA;
private Vector3 posB;
private Vector3 nexPos;
public GameObject Player;
[SerializeField]
private float speed;
[SerializeField]
private Transform childTransform;
[SerializeField]
private Transform transformB;
void Start()
{
posB = childTransform.localPosition;
posB = transformB.localPosition;
nexPos = posB;
}
// Update is called once per frame
void Update()
{
Move();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Player")
{
collision.collider.transform.SetParent(transform);
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Player")
{
collision.collider.transform.SetParent(null);
}
}
private void Move()
{
childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nexPos, speed * Time.deltaTime);
if (Vector3.Distance(childTransform.localPosition,nexPos) <= 0.1)
{
ChangeDestination();
}
}
private void ChangeDestination()
{
nexPos = nexPos != posA ? posA : posB;
}
}
Move(); should be executed in lateupdate, because otherwise you will set the position of the player and later the physics system will update it, causing stuttering.
Another solution would be raycasting straight down and placing your GameObject on the collision point (plus some kind of offset)

Why does my charakter in unity gets stuck in the tilemap?

In my Unity my Player gets everytime stuck in the collider when he has a big force (If he falls from a high place, The Gravity Scale or his jumppower is high). In the picture you can see Colliders and Renderer.
Only one Component is not on in screen at the platformcomponents, Tilemap.
Here is my code for Playermovement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private LayerMask platformsLayerMask;
private Rigidbody2D rigidbody2d;
private BoxCollider2D boxCollider2d;
private void Awake()
{
rigidbody2d = transform.GetComponent<Rigidbody2D>();
boxCollider2d = transform.GetComponent<BoxCollider2D>();
}
private void Update()
{
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
float jump_power = 17.5f;
rigidbody2d.velocity = Vector2.up * jump_power;
}
HandleMovement();
}
private bool IsGrounded()
{
RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, .1f, platformsLayerMask);
return raycastHit2d.collider != null;
}
private void HandleMovement()
{
float moveSpeed = 10f;
if (Input.GetKey(KeyCode.A))
{
rigidbody2d.velocity = new Vector2(-moveSpeed, rigidbody2d.velocity.y);
}
else if (Input.GetKey(KeyCode.D))
{
rigidbody2d.velocity = new Vector2(+moveSpeed, rigidbody2d.velocity.y);
}
else
{
rigidbody2d.velocity = new Vector2(0, rigidbody2d.velocity.y);
}
}
}
Have you tried setting the player's rigidbody collision detection to continuous? If the collision detection is set to discrete, it's really easy to come accross this kind of problem when using the built in physics system

Click on active surface to place GameObject with 8th Wall XR?

With 8th Wall XR is it possible to click on a surface and make it the active surface and place a gameobject on the position of the click? Kinda like ARKit which only augments the gameobject after clicking on it.
Something like this should do the trick. You'll need a GameObject (i.e. a Plane) with an XRSurfaceController attached, and put it on a Layer called "Surface":
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaceObject : MonoBehaviour {
// Adjust this if the transform isn't at the bottom edge of the object
public float heightAdjustment = 0.0f;
// Prefab to instantiate. If null, the script will instantiate a Cube
public GameObject prefab;
// Scale factor for instantiated GameObject
public float objectScale = 1.0f;
private GameObject myObj;
void Update() {
// Tap to place
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began ) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
// The "Surface" GameObject with an XRSurfaceController attached should be on layer "Surface"
// If tap hits surface, place object on surface
if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Surface"))) {
CreateObject(new Vector3(hit.point.x, hit.point.y + heightAdjustment, hit.point.z));
}
}
}
void CreateObject(Vector3 v) {
// If prefab is specified, Instantiate() it, otherwise, place a Cube
if (prefab) {
myObj = GameObject.Instantiate(prefab);
} else {
myObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
}
myObj.transform.position = v;
myObj.transform.localScale = new Vector3(objectScale, objectScale, objectScale);
}
}