- Home /
Raycast doesn't collide as it should
Hi guys, today I want to present a problem that does not allow me to advance in the development of my game, it is a TPS-style camera, which uses a raycast to detect collision, but instead of doing it well, it only collides for on one side and not on the other. For example, if I have a cube, it will only collide on one side of the face and for the other 3 no, how strange isn't it? This is the code I use:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerCamera : MonoBehaviour {
     public Transform target;
     public float distance = 3.0f;
     public float maxDistance = 4.0f;
 
     public float xSpeed = 250.0f;
 
     private float x = 0.0f;
     private float y = 0.0f;
 
     void Start()
     {
         var angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
 
         // Make the rigid body not change rotation
         if (GetComponent<Rigidbody>())
         {
             GetComponent<Rigidbody>().freezeRotation = true;
         }
     }
     
     void FixedUpdate()
     {
         RaycastHit hit = new RaycastHit(); Ray ray = new Ray(transform.position, target.position);
         Debug.DrawLine(transform.position, target.position, Color.red);
         if (Physics.Raycast(ray, out hit, maxDistance))
         {
             distance = hit.distance + 1f;
             print("Tocaste la pared!");
         }
         else print("Dejaste la pared :(");
     }
 
     void Update()
     {
         if (target)
         {
             x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
 
             var rotation = Quaternion.Euler(y, x, 0);
             var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
 
             transform.rotation = rotation;
             transform.position = position;
         }
     }
 }
Could you give me a hand? thanks :D
Answer by xxmariofer · Aug 06, 2019 at 10:14 PM
the most classic error i have seen in unity answers, debug.drawline gets 2 points for the ray, while ray gets a vector(start position) and a direction test this code in the ray. take into account you will need to calculate the Vector3.Distance if it is needed in your code
 Vector3 direction = target.position - transform.position;
 Ray ray = new Ray(transform.position, direction);
It doesn't work unfortunately, I'm going crazy with this ...
i have just tested in case i was missing something but it is working 100% for me. whats your exact issue?
         Vector3 direction = target.position - transform.position;
         Ray ray = new Ray(transform.position, direction);
         RaycastHit hit = new RaycastHit(); //Ray ray = new Ray(transform.position, target.position);
         Debug.DrawLine(transform.position, target.position, Color.red);
         if (Physics.Raycast(ray, out hit, 100))
         {
             //distance = hit.distance + 1f;
             print("Tocaste la pared!");
         }
         else print("Dejaste la pared :(");
Your answer
 
 
             Follow this Question
Related Questions
How to "Camera.ScreenPointToRay". I get an error in MonoDevelop. 1 Answer
Object reference not set to an instance of an object 2 Answers
Camera bounce back? 0 Answers
RaycastError 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                