Things will be much simpler if you create another GameObject, put a GameObject with bottle model inside it, and adjust position and rotation of inner GameObject so the bottle should aim straight at its center. Then you can just set position of that outer GameObject to the point where the bottle should aim, and it will aim correctly with any rotation of outer object.
If you want to alter distance from bottle to target, change localPosition
of inner object (most probably only z).
Okay, I made a working example.
There are:
- man - the object where bottle should aim
- botl - the object which contain elements of the bottle.
- aimed botl - the object which contains the bottle rotated and moved to aim at local (0, 0, 0). The distance to target also adjusted here.
There is a script on aimed botl based on your script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public Transform target;
public float xSpeed, ySpeed;
float x, y;
void Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void Update () {
if (target) {
x += Input.GetAxis ("Mouse X") * xSpeed;
y += Input.GetAxis ("Mouse Y") * ySpeed;
y = Mathf.Clamp (y, -30, 75);
var rotation = Quaternion.Euler (y, x, 0);
transform.SetPositionAndRotation (target.position, rotation);
}
}
}
Transform is set to sphere of the man.
xSpeed is -5, ySpeed is 2.
That's all.