- Home /
Camera orbit and object following
I have two questions so ill just put both here- The first question i have is i have a script that i put on my camera that will allow me to look around it when you move the mouse but my issue is that it wont move up and down only around the object here is the script -
using UnityEngine;
using System.Collections;
public class Orbit : MonoBehaviour {
public float turnSpeed = 4.0f;
public Transform player;
public float height = 1f;
public float distance = 2f;
private Vector3 offsetX;
private Vector3 offsetY;
void Start () {
offsetX = new Vector3 (0, height, distance);
offsetY = new Vector3 (0, 0, distance);
}
void LateUpdate(){
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.right) * offsetY;
transform.position = player.position + offsetX;
transform.LookAt(player.position);
}
}
Question two is i have it so when you click on an object you can move it around so its like telekinesis but if i move my mouse under the terrain it will just go right through the terrain and if i move my mouse real quick and let go it will just drop from when i droped it from instead of flinging out i want a way so instead of just plain moving it it will make the object follow so that it will try to go unter the terrain but it wont any help?
Please check the $$anonymous$$ouse axis that you are checking in the LateUpdate() method. Input.GetAxis("$$anonymous$$ouse X") is checked in both cases (for vertical and horizontal movement. (Or the na$$anonymous$$g of the variables is misleading/wrong)
As far as looking around using the mouse goes, I'm pretty sure Unity has a script just for this already. $$anonymous$$ight be called $$anonymous$$ouseLook.cs?
As for being able to move the object around, does the object have a rigidbody? I'm betting you're changing the transform.position value. This means that you are effectively teleporting the object to a new location when it moves. So you're forcing it through other colliders like the ground.
Perhaps try adding a rigidbody to your object and moving it using forces?
As for throwing the object, when you let go apply a force or set the rigidbody's velocity to some value. Since it doesn't have a force being applied to it, and it doesn't have a velocity, it just drops straight down when you let go of it.
You see red i made my own that is a 3rd person camrea that will allow you to look around your character without changing anything but the cameras position it is a nice script that i would like to keep using. As for the moving of the object i was using raycasting to move it around all i want is for the object to follow the mouse ins$$anonymous$$d of tping right to it and that should fix the moving of the object as well because if i gt enough speed whith it following the mouse then i should just be able to let go and have it fling
So you're casting a ray from the mouse to the object and moving it along said ray?
You could try using a Vector3.$$anonymous$$oveTowards or a Vector3.Lerp to make the object move towards the mouse position, and then when you let go "turn off" your movetowards or lerp.
Alright... im decently new to unity tho do you think you can help? I have no clue on how to do that.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to properly convert mouse pos to world pos 1 Answer
OnMouseUp() Click Display Effect. 1 Answer
How to rotate an object around another facing to mouse? 1 Answer
newbie: drag a rigidbody with mouse 2 Answers