Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CsOmega · Sep 05, 2017 at 11:11 AM · rotationcamera rotateturrets

How to make a turret follow the center of the screen when it's rotate ?

I've asked something similar 2 weeks ago but nobody answered.I've followed a youtube video for making a camera that rotates around an gameobject.The gameobject is a spaceship with turrets and i want the turrets to follow like the middle of the screen when i move the camera.I want it only follow on y axes so it's rotates like a tank turret . This is the camera rotation script using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ThirdPersonCamera : MonoBehaviour { private const float Y_ANGLE_MIN = -35.0f; private const float Y_ANGLE_MAX = 50.0f;

 public Transform lookAt;
 public Transform camTransform;

 private Camera cam;

 public float distance = 10.0f;
 private float currentX = 0.0f;
 private float currentY = 0.0f;
 public float SensivityX = 60.0f;
 public float SensivityY = 30.0f;

 private void Start()
 {
     Cursor.visible = false;
     camTransform = transform;
     cam = Camera.main;
 }

 private void Update()
 {
     Cursor.visible = false;
     currentX += Input.GetAxis ("Mouse X");
     currentY += Input.GetAxis ("Mouse Y");
     currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
 }

 private void LateUpdate()
 {
     Vector3 dir = new Vector3 (0, 0, -distance);
     Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
     camTransform.position = lookAt.position + rotation * dir;
     camTransform.LookAt (lookAt.position);
 }

} so if you doesn't understand i want somehow to link the turret rotation with the camera rotation so if i look back the turret rotates idk 180 degrees. I had a script that do that but it wasn't linked to the camera so if i turned the SHIP to the right let's say 50 degrees the turret rotates with the ship but the camera stays and disturb everything here is the script of the turret using System.Collections; using System.Collections.Generic; using UnityEngine;

public class turretTest : MonoBehaviour {

 public float speed = 1f;
 public float fireRate = 1f;
 private float fireCountdown = 0f;

 public GameObject LaserPrefab;
 public Transform firePoint;

 void Update () 
 {
     if (Input.GetAxis ("Mouse X") < 0 || (Input.GetAxis ("Mouse X") > 0)) 
     {
         transform.Rotate(0, (Input.GetAxis("Mouse X")), 0 * Time.deltaTime * speed );
     }

     if (Input.GetMouseButton(0))
     {
         if (fireCountdown <= 0f) 
         {
             Shoot ();
             fireCountdown = 1f / fireRate;
         }

         fireCountdown -= Time.deltaTime;
     }
 }

 void Shoot ()
 {
     Instantiate (LaserPrefab, firePoint.position, firePoint.rotation);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jiak1 · Sep 06, 2017 at 09:50 PM

In your third person camera class couldn't you just apply the rotations to the barrel as well as the camera? E.G:

  public class ThirdPersonCamera : MonoBehaviour
  { 

  private const float Y_ANGLE_MIN = -35.0f;
  private const float Y_ANGLE_MAX = 50.0f;

  public Transform lookAt;
  public Transform camTransform;
  private Camera cam;
  public float distance = 10.0f;
  private float currentX = 0.0f;
  private float currentY = 0.0f;
  public float SensivityX = 60.0f;
  public float SensivityY = 30.0f;

  public Transform barrelTrans;

  private void Start()
  {
      Cursor.visible = false;
      camTransform = transform;
      cam = Camera.main;
      //TODO Set barrelTrans to the barrel transform
  }
  private void Update()
  {
      Cursor.visible = false;
      currentX += Input.GetAxis ("Mouse X");
      currentY += Input.GetAxis ("Mouse Y");
      currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
  }
  private void LateUpdate()
  {
      Vector3 dir = new Vector3 (0, 0, -distance);
      Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
      camTransform.position = lookAt.position + rotation * dir;
      camTransform.LookAt (lookAt.position);
      
      barrelTrans.LookAt(lookAt.position);
  }

I haven't tried this cause i'm not at home but in theory it should work and if you only want the y axis then only set the Y.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CsOmega · Sep 07, 2017 at 07:49 AM

It doesn't work and it can only be used for one turret but thank you for you're time !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make the player rotate to where an object is being thrown? 1 Answer

How do I rotate player's camera to peak around objects. (first person shooter leaning) 1 Answer

Unity Rotate Around and Orbit Question 0 Answers

How to rotate a turret around y axis using the mouse ? 0 Answers

Getting Bullets to fire in Direction of Gun Barrel 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges