1

I'm currently working on my first Unity Learn code challenge, making a Flappy Bird style game where a plane needs to fly through gaps between wall obstacles.

I've successfully implemented all the required features except for one: I want to add colliders to my obstacle prefabs, but I'm having trouble making them work. I've added colliders and a rigidbody, but the plane continues to fly right through the walls instead of stopping.

I utilized three scripts: PropellerSpin, PlayerController, and FollowPlayer for the camera.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PropollerSpin : MonoBehaviour { public GameObject propeller; public float rotationSpeed = 10000f;

 // Update is called once per frame
void Update()
{
    transform.Rotate(rotationSpeed * Time.deltaTime * 
    Vector3.forward);
}

}

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class PlayerControllerX : MonoBehaviour
{
     public float speed = 15.0f;
     public float rotationSpeed = 15.0f;
     public float verticalInput;
     //public float forwardInput;


     // Update is called once per frame
    void FixedUpdate()
    {
        // get the user's vertical input
        verticalInput = Input.GetAxis("Vertical");
        //forwardInput = Input.GetAxis("Vertical");



        // move the plane forward at a constant rate
        transform.Translate(speed * Time.deltaTime * Vector3.forward);

        // tilt the plane up/down based on up/down arrow keys
        transform.Rotate(rotationSpeed * Time.deltaTime * verticalInput * Vector3.right);
    }
 }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayerX : MonoBehaviour
{
     public GameObject plane;
     private Vector3 offset = new(30, 2,-1);

     // Update is called once per frame
    void LateUpdate()
    {
        transform.position = plane.transform.position + offset;
    }
}

Game and scene view Hierarchy and Inspector windows

DMGregory
  • 134,153
  • 22
  • 242
  • 357
Mila Data
  • 13
  • 5
  • I think your issue might be the same as the one described in this question: Car driving through walls with Transform.Translate. Moving a physics object with its transform component is not appropriate if you want the physics engine to be able to interrupt that movement. Use the Rigidbody. – DMGregory Aug 04 '23 at 15:49
  • Have you had any luck applying the fixes recommended in the linked Q&A, or do you need any specific help with it? – DMGregory Aug 05 '23 at 15:43
  • I haven't tried it yet. The rigidbody from the editor didn't work; it appeared below the plane, and it's supposed to cover the shapes of the obstacles, right? The weird thing is that when I touch it to modify the scale or position, it touches the obstacles but makes no changes to them. – Mila Data Aug 06 '23 at 03:34
  • Rigidbodies have no visual appearance or spatial extents of their own, so I don't know what you mean by "it appeared below the plane" or "it's supposed to cover the shapes of the obstacles" — are you talking about a collider component of some kind instead? Again, remember that "doesn't work" or "no change" are not enough information to document an issue: what change were you expecting? How did the observed results differ from that expectation? – DMGregory Aug 06 '23 at 07:02
  • I'll come back to this project soon, I'll let you know when. Thank you very much, It seems I was confusing Rigidbodies with colliders then, I'll see it soon. – Mila Data Aug 14 '23 at 15:40

0 Answers0