Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Unity Help - Changing Axis of object

  • 04-04-2016 1:42pm
    #1
    Registered Users Posts: 101 ✭✭


    Hey all,
    I have to do a games assignment where you play as a robot. I am trying to have it so the camera follows the robot as it moves around and the camera is positioned behind the robot. While I have the camera done, I have the problem where my robot's axis is different, so the camera perspective is from the side of the robot, rather than the back. I think the camera is thinking the back of the robot is the Z Axis, while the actual back of the robot is the X axis. I feel this can easily be changed but I don't know how. Anyone have an idea how to fix this? Here is my code for the camera:
    using UnityEngine;
    using System.Collections;

    public class FollowCamera : MonoBehaviour {
    public GameObject target;
    public float damping = 3;
    Vector3 offset ;

    void Start() {
    offset = target.transform.position - transform.position;
    }

    void LateUpdate() {
    float currentAngle = transform.eulerAngles.y;
    float desiredAngle = target.transform.eulerAngles.y;
    float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime * damping);

    Quaternion rotation = Quaternion.Euler(0, angle, 0);
    transform.position = target.transform.position - (rotation * offset);

    transform.LookAt(target.transform);
    }
    }

    Thank you for helping and I'm sorry if I'm not clear on this topic.


Comments

  • Registered Users Posts: 32 oisincar


    This code looks like it takes the starting position of the camera. It's nothing to do with axis as far as i can tell.

    Just move the camera to the correct position in the viewport and you should be golden.


  • Registered Users Posts: 175 ✭✭Withrax


    A real simple way to do this would be make the camera a child of the robot in the hierarchy. No code needed for that. But I guess you might need the camera to lag behind a bit?


  • Registered Users Posts: 101 ✭✭PKdude


    Thanks for the help guys! I have gotten it to work but now, the camera will just rotate very quickly around the model. I even tried a model whose axis didn't have to be changed but it still reacted the same. Any idea why this could be?


  • Registered Users Posts: 175 ✭✭Withrax


    offset should be the other way around:

    offset = transform.position - target.transform.position;

    I can't really figure out what your code in lateupdate is doing. A simple way of doing it would be

    void LateUpdate()
    {
    Vector3 targetCameraPosition = target.transform.position + offset;
    transform.position = Vector3.Lerp (transform.position, targetCameraPosition, damping * Time.deltaTime);

    Quaternion neededRotation = Quaternion.LookRotation(target.transform.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, Time.deltaTime * rotationSpeed);
    }

    rotationSpeed seems ok around 10

    if you want to fix the messed up axis in your imported game object just create an empty game object and make the imported one a child of the empty one


  • Registered Users Posts: 101 ✭✭PKdude


    I have to thank you so much, I was tearing my hair out at this! The camera is focusing behind the robot now, thanks very much. I am now trying to have it so when you turn the robot, the camera turns with it. Or, Out of curiosity, would there be a way to turn the camera with an input? Like using the left and right arrow keys to move the camera left and right and then use WASD to move the robot? Or would that require another script?


  • Advertisement
  • Registered Users Posts: 175 ✭✭Withrax


    No problemo. You could just drag and drop the camera on to the robot. The camera will move with the robot and rotate with it too. Make sure you remove the camera script tho.

    If you want to rotate the camera with input you will need a separate script with something like

    Update()
    {
    float h = Input.GetAxisRaw ("Horizontal");
    TurnCamera();
    }

    void TurnCamera()
    {
    transform.Rotate (Vector3.up, (h * speed) * Time.deltaTime);

    }


  • Registered Users Posts: 101 ✭✭PKdude


    Oh god I'm so stupid XD Thank you so much for all of your help! You are so awesome to help me!


Advertisement