- Home /
The question is answered, right answer was accepted
How to change forward direction of ball to where camera is facing?
Hi,I know there are already a number of questions related to object facing camera look,but none have quite the answers for me though,I am still a newbie though so I must have not really understood them unfortunately,maybe with a little detail from my side of story could help me. Okay,so i got the camera to follow my rolling object,n got the mouse look script,first ill show the rolling ball script: #pragma strict
function Start () {
}
var PlayerJumpSpeed : float = 5.0;
var MaxJumps : float = 30.0;
var PlayerRollSpeed : float = 100.0;
var rotationSpeed : float = 30.0;
var maxRadiansDelta : float = 30.0;
var amount : float = 0.30;
var movementSpeed : float = 20.0; /* Block speed */
function FixedUpdate ()
{
var movement = (Input.GetAxis("Horizontal") * -Vector3.left * movementSpeed)+(Input.GetAxis("Vertical") * Vector3.forward * movementSpeed);
rigidbody.AddForce(movement, ForceMode.Force);
// move player by letting them roll the ball
//var rot :float = Input.GetAxis("Horizontal") * Time.deltaTime;
//rigidbody.transform.Rotate(0,rot,0);
//Vector3.RotateTowards(Vector3.left,Vector3.left,maxRadiansDelta,maxMag);
//rigidbody.AddForce(Vector3.Cross(Vector3.left,Vector3.forward) * amount);
var torque = Vector3(Input.GetAxis("Vertical"), 0, -Input.GetAxis("Horizontal"));
//torque = Camera.main.transform.TransformDirection(torque);
//torque.y = 0;
rigidbody.AddTorque(torque.normalized*movementSpeed);
/*if (Input.GetKeyUp("space") && Physics.Raycast(transform.position, Vector3.down, MaxJumps))
{
rigidbody.AddForce(Vector3.up * PlayerJumpSpeed, ForceMode.Impulse);
}*/
}
this is the camera follow script: /* This camera smoothes out rotation around the y-axis and height. Horizontal Distance to the target is always fixed.
There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.
*/
// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
var cameraObject : GameObject;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
//transform.LookAt (target);
}
and both scripts were of course obtain from parts of code of other places i searched. Now the mouse look code: using UnityEngine; using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = false;
}
}
So now,with all these in place i need help of how to make the ball object turn,or its forward direction faces according to where the camera faces,thank you,it would help for a detailed explanation,im still new.
https://www.youtube.com/watch?v=blO039OzUZc&t=394s i used this camera but then u lose rotation option of sphere
Follow this Question
Related Questions
Typecasting Camera to Object for ObjectField 3 Answers
Change characters direction using camera view 0 Answers
How can I change camera when colliding with "X" object? 1 Answer
Measuring XYZ coordinates in Unity vs in real life. 1 Answer
How do you position an object relative to the camera? 0 Answers