- Home /
misaligned mouse follow, with VIDEO
So here's a video of what's happening to it. It occurs from even small bumps sometimes. http://youtu.be/EJlbAMo4NHU So I have a script to make an onboard turret follow the mouse. Code Block A:
public function setaim()
{
aimingArm.transform.localPosition = Vector3(0,0,0);
ray = camcam.ScreenPointToRay(Input.mousePosition);
p = ray.GetPoint(32);//was 132
dir1 = (this.transform.position.x - p.x);
dir2 = (this.transform.position.y - p.y);
angle = Mathf.Atan2(dir1, dir2) * Mathf.Rad2Deg;
aimingArm.transform.LookAt(aimPoint.transform.localPosition);
aimingArm.transform.eulerAngles = Vector3(0,0,angle * -1);
}
black ball = character. Purple ball = mouse cursor. So what happens is that the gun(aimingarm) will start to look at the yellow line. That's the dark blue gun, where it starts. When I pull the trigger, it momentarily bounces to where it's supposed to be looking, green line, aqua gun. Then bounces back. This is not predictable except that it happens a little more following collisions. I checked that the camera isn't tilted, and that the gun isn't tilted. The overall effect is that while it shoots where it's supposed to go 90% of the time, there is that 10% and the epileptic cannon.
setaim() is called for mouse follow regularly, as well as the moment before the gun fires. aimingarm attaches via configurable joint on pickup, which sets position, anchors and constraints in it's onCollision call. It kind of seem like Input.mousePosition doesn't provide proper numbers until a click occurs. What's going on how do I fix it?
Thanks guys and gals!
When you say regularly, do you mean inside Update()
?
This probably won't fix your problem, but having both LookAt()
and setting eulerAngles
might be redundant. As far as I know, LookAt()
is just a convenient way of rotating the positive z-axis to point towards something (it does the math for you). Setting eulerAngles
right after would overwrite any changes that LookAt
does, since it also sets the orientation of the transform.
First off, thank you very much. regularily = inside fixedUpdate() and each time when the trigger is pressed, before shots are launched and recoil is applied. I removed the Eulers, and it brought me back to the original issue. When it's just lookAt(p), the gun is locked facing down and it sling my character about wildly(which is in my redacted comments if I actually read them...That's actually explained well enough by what you just told me. Since Eulers set the orientation. I've been reading and working on this, and this is where I am at....
public function setaim()
{
ray = camcam.ScreenPointToRay(Input.mousePosition);
//cam behind, use + cam for position, - caused revered y co ordiantes
p = ray.GetPoint( ai$$anonymous$$gArm.transform.position.z + camcam.transform.localPosition.z);//was 32 flat
dir1 = (ai$$anonymous$$gArm.transform.position.x - p.x)*-1;//x reveresed when set to localEulers
dir2 = (ai$$anonymous$$gArm.transform.position.y - p.y);
angle = $$anonymous$$athf.Atan2(dir1, dir2) * $$anonymous$$athf.Rad2Deg;
//ai$$anonymous$$gArm.transform.LookAt(p);//behaves incorrectly.
ai$$anonymous$$gArm.transform.localEulerAngles = Vector3(0,0,angle);
}
switching to localPosition and localAngels has reduced the size of the error, adn it slowly works it's way back in after the mouse is moved alot and the gun fires...
by printing the output of the angels, it appears that the gun is tilting backward, despite having projection set, and again in the eulers...
I am so at a loss.
You might want to do this inside of Update()
or LateUpdate()
, which are executed every frame. I don't think FixedUpdate()
gets executed every frame.
Your answer
Follow this Question
Related Questions
gameobject follow mouse with on mouseclick instantiate 0 Answers
how to make a charecter controller 1 Answer
Stop object rotation 1 Answer
Mouse follow performance in iPhone 1 Answer