Another little problem with my railshooter script

Hi all,
making progress but I’m stumped again.
My gryocopter refuses to roll. I have the pitch and yaw working fine, but it flat out refuses to roll. Can someone look over my script and tell me where I went wrong?

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

public class gyroplayer : MonoBehaviour
{
[Tooltip(“In meters per second”)][SerializeField] float speed = 20f;
[Tooltip(“In meters”)] [SerializeField] float xRange = 10f;
[Tooltip(“In meters”)] [SerializeField] float yRange = 5f;

[SerializeField] float positionPitchFactor = 2f;
[SerializeField] float controlPitchFactor = -20f;
[SerializeField] float positionYawFactor = -1.5f;
[SerializeField] float controlRollFactor = -5f;

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()
{
    float xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
    float yThrow = CrossPlatformInputManager.GetAxis("Vertical");

    float xOffset = xThrow * speed * Time.deltaTime;
    float yOffset = yThrow * speed * Time.deltaTime;

    float rawXPos = transform.localPosition.x + xOffset;
    float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

    float rawYPos = transform.localPosition.y + yOffset;
    float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

    transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
}

}

can anyone help? I’m banging my head on the desk here :smiley:

Hi Andy,

Please format your code properly.

Have you checked your values in the Inspector? Have you rewatch the video at least one more time? Have you tried to comment out the pitch and the yaw to make the roll work independently?


See also:

Hi Nina,
I have tried playing about with the settings in my inspector, and just get wild results for pitch and yaw, nothing at all seems to affect the roll. I tried commenting the code for pitch and yaw out, but roll still stubbornly refuses to work. I’ve watched that section of the lesson about 20 times now, and am at a loss!
here is my processRotation

 private void processRotation()
    {
       
 //Pitch and yaw
        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);

I have attached a screenshot of the inspector too, maybe it helps?

THANK YOU!!!

Hi guys, still struggling with this…anyone able to assist?

Set pitch and yaw to 0 in your processRotation method, and log the roll value into your console. This way, you can see whether the roll itself works. If it does, test yaw and pitch separately.

If all of them work, combine two. If they work, add the third one.

This way, you can narrow down the problem. At the moment, there are three different things that could cause the issue, and it is impossible to tell which one it is, or if two or three are causing the issue, or the combination of the three.

Hi Nina,
okay, so I did that, and the console records a roll of zero, so it’s definitely not doing anything at all. Pitch and yaw work just fine, simply no roll at all.

Any thoughts?

For what exactly does the console show 0? For float roll = xThrow * controlRollFactor;? If so, log the two variables on the right-handed side of the equal/assign sign into your console. One or both of them must be 0.


Oh, I think I’ve spotted the issue. Do you know what a local variable is? If not, please look it up after you figured out which variable is 0.

I’m surprised that pitch and yaw work.

How are you getting on with this, @AndyW?

This topic was automatically closed after 43 hours. New replies are no longer allowed.

Privacy & Terms