Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
CakeMonsTv

Camera moving relative to its look direction

Question

Hi, can someone help me with my camera script please? Im trying to make so that when i press W, camera will move in that direction and y wont change. (That goes for all W,S,A and D keys, S backwards, A left... )

This is my code rn:

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

public class CameraControl : MonoBehaviour {

    public float Border = 20f;
    public float rotationSpeed = 250f;
    public float moveSpeed = 20f;
    public float rot; 
    public float rot1; 
    public float zoomSpeed = 20f;
    public float minY = 5f;
    public float maxY = 65;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update () {
        Vector3 pos = transform.position;

        if (Input.GetButton ("W")) {
            pos.z += moveSpeed * Time.deltaTime; //modify z component (move forward), time.deltatime manages movement if FPS varies. add movespeed to pos.z
            if (Input.GetButton ("Shift")) {
                moveSpeed = moveSpeed * 2;
            }
        }
        if (Input.GetButton ("A")) {
            pos.x -= moveSpeed * Time.deltaTime; //modify z component (move forward), time.deltatime manages movement if FPS varies. add movespeed to pos.z

        }
        if (Input.GetButton ("S")) {
            pos.z -= moveSpeed * Time.deltaTime; //modify z component (move forward), time.deltatime manages movement if FPS varies. add movespeed to pos.z

        }
        if (Input.GetButton ("D")) {
            pos.x += moveSpeed * Time.deltaTime; //modify z component (move forward), time.deltatime manages movement if FPS varies. add movespeed to pos.z

        }

        if (Input.GetButton ("E")) {
            rot += rotationSpeed * Time.deltaTime; //rotation value
            transform.eulerAngles  = new Vector3(36, rot, 0);
        }
        if (Input.GetButton ("Q")) {
            rot -= rotationSpeed * Time.deltaTime; //rotation value
            transform.eulerAngles  = new Vector3(36, rot, 0);
        }

        float scroll = Input.GetAxis ("Mouse ScrollWheel"); //Get Input
        pos.y -= scroll * zoomSpeed * 100f * Time.deltaTime; //Camera zoom
        pos.y = Mathf.Clamp (pos.y, minY, maxY); //limits camera zoom

            transform.position = pos;
    }
}
 

I basicly dont want this to happen: Please login or register to see this link.

Edited by CakeMonsTv
Extra information

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1

by using TransformDirection and zeroing out the y direction. Also fixed your zooming because it's messed up as well (unless your actually wanting it to go straight up and down instead of forward zooming):

 

 

 

void Update()

{

//Move Speed Stuff

float moveSpeed = 10;

if(Input.GetKey(KeyCode.LeftShift)) moveSpeed = 20;

 

//rotation stuff goes here

 

//Wasd in world space

Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

 

//transformed to local space with zeroed y

moveDirection = transform.TransformDirection(moveDirection);

moveDirection.y = 0;

 

//and move in that direction smoothly

transform.position += moveDirection * moveSpeed * Time.deltaTime;

 

//and zoom stuff

Vector3 zoomPos = transform.position - (transform.forward * Input.GetAxis("MouseScrollWheel") * zoomSpeed * 100.0f * Time.deltaTime);

if(zoomPos.y >= minY && zoomPos.y <= maxY) transform.position = zoomPos;

}

Share this post


Link to post
Share on other sites

  • 1

Changed it several times incase you didn't see that. Forgot in c# you can't manipulate each value separately. I'm done changing it tho

Share this post


Link to post
Share on other sites
  • 0

Thank you! Works like a charm now. I just had to invert mouse scrollwhell input since it needed to be inverted.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...