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 /
This question was closed Aug 02, 2017 at 05:15 PM by BozeMax for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BozeMax · Aug 02, 2017 at 01:01 PM · cameratransformlookat

make player move in camera direction

Hey everyone, i'm making a roll a ball game and i followed the unity tutorial on making it. I'm fairly new to coding so this is getting a little complicated for me. There's probably allready an answer but i can't figure out how to implement that into my code...

I've also followed a third person camera tutorial so i can orbit the ball im controlling, but the movement is off course not following the direction the camera is facing. if anyone has an idea how to get the ball to roll into the direction the camera is facing that would be great :) here's the playercontroller code:

     public class Playercontroller : MonoBehaviour {
     
     public Text winText;
     public Text countText;
     public float speed;
     private Rigidbody rb;
     private int count;
 
 
     void Start()
     {
         rb = GetComponent<Rigidbody> ();
         count = 0;
         countText.text = "Score: " + count.ToString ();
         winText.text = "";
     }
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         rb.AddForce (movement * speed);
     }
 
 
     void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ("Pick Up")) {
             StartCoroutine (destroyOther (other));
         }
     }
     IEnumerator destroyOther(Collider other)
     {
         Material mat = other.GetComponent<Renderer> ().material;
         Color matC = mat.color;
         float t = 0f;
         while(t < 1f){
             t += Time.deltaTime / 0.2f;
             yield return 0;
             float a = Mathf.Lerp (1f,0f, t );
             matC.a = a;
             mat.color = matC;
         }
 
         other.gameObject.SetActive (false);
         count = count + 1;
         countText.text = "Score: " + count.ToString ();
         if (count >= 13)
         {
             winText.text = "You Win!";
         }
 
     }
 
 }


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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by $$anonymous$$ · Aug 02, 2017 at 01:15 PM

The Problem Isn't With The ThirdPersonCamera Script, You Have To Go To The Ball Controller Script, But You Can Simply Write This Line In The Update Function/Void Of The Ball Script:

  public enum MovementType {Force,Torque}
      public float speed = 25f;
      public MovementType movementType = MovementType.Torque;
      private Vector3 forward = Vector3.zero;
      private Vector3 movement = Vector3.zero;
      private Rigidbody rb = null;
      void FixedUpdate ()
      {
          float moveHorizontal = Input.GetAxis("Horizontal");
          float moveVertical = Input.GetAxis("Vertical");
          forward = Vector3.Scale(Camera.main.transform.forward,new Vector3(1,0,1)).normalized;
          movement = (moveVertical * forward + moveHorizontal * Camera.main.transform.right).normalized;
          if(movementType == MovementType.Force)rb.AddForce(movement * speed);
          if(movementType == MovementType.Torque)rb.AddTorque(new Vector3(movement.z,0,-movement.x) * speed);
      }

NOTE: The Answer Is Updated. Hope It Works :D

Comment
Add comment · Show 21 · 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 BozeMax · Aug 02, 2017 at 01:27 PM 0
Share

i changed the code into the playercontroller code, hope that is of some help. do i need to add a void Update to this script? because i dont think i can just put it somewhere in fixedUpdate

avatar image $$anonymous$$ BozeMax · Aug 02, 2017 at 01:32 PM 0
Share

You Can Put It In:
Update()
FixedUpdate()
LateUpdate()
Or Any Functions/Voids That Get Called By These 3 Functions/Voids

avatar image BozeMax $$anonymous$$ · Aug 02, 2017 at 01:36 PM 0
Share

as you can see i put it in void FixedUpdate

void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rb.AddForce (movement * speed); transform.eulerAngles.y = Camera.main.transform.eulerAngles.y; }

but i get this error: Assets/Scripts/Playercontroller.cs(28,25): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.eulerAngles'. Consider storing the value in a temporary variable

Show more comments

Follow this Question

Answers Answers and Comments

113 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 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

LookAt only when camera position changes 2 Answers

An alternative to RotateAround for collisions 0 Answers

transform.lookat() limitation... 1 Answer

Having a plane at exactly the far clip of a camera 1 Answer

Attaching camera to instantiated object 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