Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:29:24+00:00 2026-06-06T22:29:24+00:00

Ok im new to javascript and Unity so this is clearly a very noob

  • 0

Ok im new to javascript and Unity so this is clearly a very noob beginner question.
Basically I have created a script that i place on my camera. Then when i select a model in the game it becomes the target of the camera script and im trying to make the camera smoothly turn to it. below is what im trying

in the update function i use raycast to catch a unit on the mouse click. this becomes the target variable in the script. it sets a bool to true then to follow target in the moveto function. but it only seems to move the camera if i keep the mouse left button pressed down on the unit. Im trying to select a unit then let the camera smoothly rotate to the unit.
Any ideas on this rookie mistake?

#pragma strict
var speed = 5.0;
var freeMovementTurnSpeed = 10.0;
var freeMovementForwardSpeed = 10.0;
var freeMovementVerticalSpeed = 10.0;
var maxHeight = 30;
var minHeight = 2;

var target : Transform;

var distance = 10.0;

var maxDistance = 50;
var minDistance = 10;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

// The distance in the x-z plane to the target
var followDistance = 30.0;
var maxDistanceDelta = 0.2;
var damping = 3.0;
var followTarget = false;
var smoothRotation ;
function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

    if(target){
        transform.LookAt(target);
    }
    // Make the rigid body not change rotation
    if (rigidbody)
        rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if(target){
        if(followTarget){
            moveTo();
        }
        else{
            targetSelectedMovement();
        }
    }
    else {
        freeMovement();
    }


}

function moveTo(){

    //transform.LookAt (target);

    var currentDistance = Vector3.Distance(transform.position,target.position);

    //if (currentDistance<40){
        //followTarget = false;
    //}
    //else{

        smoothRotation = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, smoothRotation, Time.deltaTime * damping);
        print(target);
        //transform.position = Vector3.MoveTowards(transform.position,target.position,maxDistanceDelta);
    //}

}

function freeMovement(){
    var zPos = Input.GetAxis("Vertical") * Time.deltaTime * freeMovementForwardSpeed;
    var xPos = Input.GetAxis("Horizontal") * Time.deltaTime * freeMovementTurnSpeed;
    var strafePos = Input.GetAxis("Strafe")* Time.deltaTime * freeMovementForwardSpeed;
    var vertPos = Input.GetAxis("GainVertical")* Time.deltaTime * freeMovementVerticalSpeed;

    if (Input.GetButton("Fire2")) {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        var rotation2 = Quaternion.Euler(y, x, 0);
        transform.rotation = rotation2;
    }
    else {
        transform.Rotate(0, Input.GetAxis("Horizontal")*freeMovementTurnSpeed*Time.deltaTime, 0,Space.World);
    }
    if (vertPos > 0 && transform.position.y  > maxHeight){
        vertPos = 0;
    }
    else if(vertPos < 0 && transform.position.y < minHeight){
        vertPos = 0;
    }

    print(transform.position.y);
    transform.Translate(strafePos,vertPos,zPos);

}
function targetSelectedMovement() {
    if(Input.GetKey("a") || Input.GetKey("d") ){

        x += (-Input.GetAxis("Horizontal")) * xSpeed * 0.02;
        var rotation = Quaternion.Euler(y, x, 0);
        var currentDistance = Vector3.Distance(transform.position,target.position);

        var position = rotation * Vector3(0.0, 0.0, -currentDistance) + target.position;

        transform.rotation = rotation;
        transform.position = position;
    }

    var zPos = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    currentDistance = Vector3.Distance(transform.position,target.position);
    if(currentDistance < maxDistance && zPos < 0){
        transform.Translate(0, 0, zPos);
    }
    else if(currentDistance > minDistance && zPos > 0){
        transform.Translate(0, 0, zPos);
    }
    if (Input.GetButton("Fire2")) {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        var rotation2 = Quaternion.Euler(y, x, 0);
        var currentDistance2 = Vector3.Distance(transform.position,target.position);

        var position2 = rotation2 * Vector3(0.0, 0.0, -currentDistance2) + target.position;

        transform.rotation = rotation2;
        transform.position = position2;
    }
}

static function ClampAngle (angle : float, min : float, max : float) {
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp (angle, min, max);
}

function Update () {

    var hit : RaycastHit;

    if (Input.GetButton("Fire1")) {

        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        // Cast a ray forward from our position for the specified distance
        if (Physics.Raycast(ray, hit))
        {
            // Add a specified force to the Rigidbody component to the other GameObject
            if(hit.collider.tag == "Unit"){
                print("Selected " + hit.collider.gameObject.name);
                target = hit.collider.gameObject.transform;
                //transform.LookAt(target);
                followTarget = true;
            }
            else {
                target = null;
            }

        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:29:26+00:00Added an answer on June 6, 2026 at 10:29 pm

    🙂

    Answer pulled from my comments above:

    “I’ll have to look when I get on my dev. machine but in the mean time try replacing Input.GetButton(“Fire1”) with Input.GetButtonDown(“Fire1″). Just as a test, because looking at your code nothing jumps out at me. GetButton is true as long as the button is held, in the past I have had issues with it incorrectly triggering just as the mouse moved off an object.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.
New to javascript, but I'm sure this is easy. Unfortunately, most of the google
Very new to javascript and html-type stuff. I wanted to just make a quick
im new to javascript and am intersted in creating a small o3d script: <!DOCTYPE
im new at javascript and i can get this slider to work but not
I have this weird problem where my Google Maps API script doesn't work, no
I am still new to Javascript and JQuery. I have written a small function
I have created dynamic elements using javascript. These elements are created when i input
Lets say I have an application that updates it javascript/css files (and keeps the
This question relates specifically to JavaScript, but I imagine would apply to any language.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.