Strange box collider issues

Hi there, I am struggling on this lecture. For some reason, my PlayerShip object already has a collider setup on it - it is an object below my PlayerShip object, and it is made of a number of sub objects with box colliders. I think this must have come in with the asset pack.

image

When Ben does this tutorial, his PlayerShip starts of with no collider, he adds the box collider component and the rigidbody component at the top “PlayerShip” level, and all works well.

When I try adding these components at top level as he does, I get a weird effect whereby when I run the game, the ship is no longer fixed relative to the camera, it moves about all over the place/disappears off of the screen.

If I try adding the rigidbody component within the Collider1/2/3/4 sub objects, when I run the game they all immediately drop away from my ship.

I tried turning “Use Gravity” off no the rigidbody elements, and this makes them stay closer, but still move about all over the place, not staying attached to the ship.

I tried deleting the existing colliders (had to be done from the prefab) and adding the box collider and rigidbody components to the top level PlayerShip, again, I get the same first weird effect of the ship not being fixed relative to the camera, and moving about all over the place/off the screen.

Not sure what to do here, any advice greatly appreciated. I might need to start again with my PlayerShip from scratch… I did try doing that, but got even more confused! I am sure there must be a reason for the behaviour I am seeing and would like to fix it from where I am!

Sorry if the description is not very good, let me know if different information would help someone troubleshoot my issue!!

Hope someone can help, many thanks in advance…

Matt

What other components/scripts are attached to the player? What does the scripts look like?

Hi there, thanks for looking at this!

The Player has the following components: Transform, Mesh Filter, Mesh Renderer, and a material (StarSparrow blue) all from the asset pack. It has the Player.cs script on it, as per the course instructions, code pasted below.

Do you think you can help from this info? Should I try to show a video of the odd behaviour, or does my description make sense?

Thanks again!

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Player : MonoBehaviour
{
//Setup editor adjustable values for x and y movement speed and range
[Tooltip(“unit - ms^-1”)] [SerializeField] float xSpeed = 40f;
[Tooltip(“unit - ms^-1”)] [SerializeField] float ySpeed = 40f;
[Tooltip(“unit - m”)] [SerializeField] float xRange = 15f;
[Tooltip(“unit - m”)] [SerializeField] float yMin = 10f;
[Tooltip(“unit - m”)] [SerializeField] float yMax = 10f;
[SerializeField] float positionPitchFactor = 0.5f;
[SerializeField] float controlPitchFactor = 20f;
[SerializeField] float positionYawFactor = -0.5f;
[SerializeField] float controlRollFactor = 20f;

//set member variables
float xThrow, yThrow;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    ProcessTranslation();
    ProcessRotation();
}

private void ProcessRotation()
{
    float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
    float pitchDueToControlThrow = yThrow * controlPitchFactor;
    float pitch = pitchDueToPosition + pitchDueToControlThrow;

    float yaw = transform.localPosition.x * positionYawFactor;

    float roll = xThrow * controlRollFactor;

    transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}

private void ProcessTranslation()
{
    //Get x & y input (x/y Throw), calculate offset to move in this frame, move ship within clamped range
    xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
    float xOffset = xThrow * xSpeed * Time.deltaTime;
    float rawNewXPos = transform.localPosition.x + xOffset;
    float newXPos = Mathf.Clamp(rawNewXPos, -xRange, +xRange);

    yThrow = CrossPlatformInputManager.GetAxis("Vertical");
    float yOffset = yThrow * ySpeed * Time.deltaTime;
    float rawNewYPos = transform.localPosition.y - yOffset;
    float newYPos = Mathf.Clamp(rawNewYPos, -yMin, +yMax);

    transform.localPosition = new Vector3(newXPos, newYPos, transform.localPosition.z);
}

}

Yeah a video might be helpful, the code looks good :+1: I’m assuming you’re adding the rigidbody so you can detect collisions? Make sure to take the gravity off and make it a kinematic object so the physics engine doesn’t take over.

Hmmmm. OK, I just started recording some videos, but… when I add box collider and rigidbody components to the PlayerShip top level but turn gravity off and kinematic on as you suggested, it all looks OK (colliders do not fall away weirdly!). Then got the console message from adding Ben’s suggested code below by turning “Is Trigger” on in the rigidbody component.

Bit confused, as I didn’t think this was what Ben did in this lecture… need to rewatch the lecture to try to understand, will do that next!

Thanks for your help!

void OnTriggerEnter(Collider other)
{
print(“Player triggered something”);
}

Rewatched. Turns out that is what Ben did. Very confusing overall though, I am sure I was getting some strange results, but now it seems to be working OK. Odd.

Interestingly I can’t get the pre-existing collider system on my player ship (the one made up of 4 sub objects, which is a better representation of the actual ship geometry) to work. These have box colliders, but if I add a rigidbody component to each and put the same settings in (“Is Trigger” on for the box collider and “Gravity” off and “Is Kinematic” on for the rigidbody), when I run the game and collide with everything I get no console print message from the code.

Any idea why that is? These colliders are not working the same as sub-objects as when the box collider and rigitbody components are added to the PlayerShip object directly. Is there something different needed in the code to access the trigger event on these sub-objects?

1 Like

Tried adding the player.cs script to the collider sub objects. This makes the trigger events work, but now the collider objects are not moving aligned with the player ship! Now why would that be…?!!

Hmmm. Think i am trying to run before I can walk with that, maybe I’d best just continue to follow the lectures now I have my build working as the course one does!!

At least I worked out how to screen cap a video, never done that before!

Haha it is a little confusing at first, there’s so much going on under the hood in Unity, just keep pressing on through the lectures and experimenting and it will just click one day, I’m still learning myself so I can’t answer all of the weird things going on :joy:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms